npm i express cors
1. 기본
const express = require('express');
var cors = require('cors');
const app = express();
const port = 3000;
app.use(cors());
app.use(express.json({
limit: "50mb"
}));
app.use(express.urlencoded({
limit: "50mb",
extended: false
}));
app.get('/*', (req, res) => {
console.log('---------get---------');
console.log('req.url', req.url);
console.log('req.query', req.query);
res.send('Hello World!');
});
app.post('/*', (req, res) => {
console.log('---------post---------');
console.log('req.url', req.url);
console.log('req.query', req.query);
console.log('req.body', req.body);
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
});
2. rawBody
const express = require('express');
const cors = require('cors');
const app = express();
const port = 3000;
app.use(cors());
app.use(function(req, res, next) {
req.rawBody = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
req.rawBody += chunk;
});
req.on('end', function() {
next();
});
});
app.get('/*', (req, res) => {
console.log('---------get---------');
console.log('req.url', req.url);
console.log('req.query', req.query);
res.send('Hello World!');
});
app.post('/*', (req, res) => {
console.log('---------post---------');
console.log('req.url', req.url);
console.log('req.query', req.query);
console.log('req.rawBody', req.rawBody);
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
});
'web > node.js' 카테고리의 다른 글
lerna test (0) | 2021.06.13 |
---|---|
node에서 python 실행 (2) | 2019.10.17 |