📝

2. express and MySQL 연동

 
npm install express --save
main.js
const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => { res.send('Hello World!') }) app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) })
 
main.js
var express = require('express') var app = express() //route, routing //app.get('/', (req, res) => res.send('Hello World!')) app.get('/', function(req, res) { return res.send('/'); }); app.get('/page', function(req, res) { return res.send('/page'); }); app.listen(3000, function() { console.log('Example app listening on port 3000!') });
 

최종코드

main.js
var express = require('express') var app = express() var fs = require('fs'); var template = require('./lib/template.js'); var mysql = require('mysql'); var db = mysql.createConnection({ host:'localhost', user:'nodejs', password:'123456', database:'testdb' }); db.connect(); //route, routing //app.get('/', (req, res) => res.send('Hello World!')) app.get('/', function(request, response){ db.query(`SELECT * FROM testtable`, function(error,notices){ var title = notices[0].title; var description = notices[0].description; var list = template.list(notices); var html = template.HTML(title, list, `<h2>${title}</h2>${description}`, `<a href="/create">create</a>` ); return response.send(html); }); }); app.get('/page', function(request, response) { return response.send('/page'); }); app.listen(3000, function() { console.log('Example app listening on port 3000!') });