[app.js]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var paintingRouter = require('./routes/painting');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/painting', paintingRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next();
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
|
[routers/users.js]
1. GET방식
1
2
3
4
5
6
7
8
9
10
|
var express = require('express');
var router = express.Router();
var db = require('../connectDB');
/* GET users listing. */
router.get('/', function(req, res, next) {
db.query('SELECT * FROM ProductTable WHERE prCode=?',req.query.num,(err, rows, fields) => {
res.send(rows);
});
});
|
2. POST방식
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
function sendPost(number){
fetch(
`/users`, // URL
{
method: 'post', // method
body: JSON.stringify({ // URL로 보낼 데이터
num: number
}),
headers:{ // json data를 보내기 위한
'Content-Type': 'application/json'
}}
).then(function(response){
return response.json();
}).then(function(json){
console.log(json);
});
}
|
[routers/users.js]
1
2
3
4
5
6
7
8
9
10
11
12
|
var express = require('express');
var router = express.Router();
var db = require('../connectDB');
/* POST users listing. */
router.post('/', function(req, res, next) {
var number = req.body.num;
db.query('SELECT * FROM ProductTable WHERE prCode=?',req.body.num,(err, rows, fields) => {
res.send(rows);
});
});
module.exports = router;
|
'NodeJS > express' 카테고리의 다른 글
express 설치 + mysql,session 모듈 추가(centos7 기준) (0) | 2020.01.28 |
---|---|
express 웹서버에서 mysql 연결 및 사용하기 (0) | 2019.09.26 |
뷰엔진을 jade에서 html로 바꾸기 (0) | 2019.09.26 |
express js 설치 시작하기(윈도우 기준) (0) | 2019.09.26 |