next 에서 주소를 동적으로 변경하기 위해 next환경에 express 연결했다.
npm i morgan express express-session cookie-parser dotenv
npm i -D nodemon
const express = require('express');
const next = require('next');
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const expressSession = require('express-session');
const dotenv = require('dotenv');
const dev = process.env.NODE_ENV !== 'production';
const app = next({dev});
const handle = app.getRequestHandler();
...
app.prepare().then(() => {
const server = express();
server.use(morgan('dev'));
server.use(express.json());
server.use(express.urlencoded({extended: true}));
server.use(cookieParser(process.env.COOKIE_SECRET));
server.use(expressSession({
resave : false,
saveUninitialized: false,
secret: process.env.COOKIE_SECRET,
cookie : {
httpOnly : true,
secure : false,
},
}));
server.get('*', (req, res) => {
// next 에서 가져온 요청 처리기
return handle(req, res);
});
server.listen(3000, () => {
console.log('next+express running on 3000');
});
});
...
"scripts": {
"dev": "nodemon",
{
"watch" : [
"server.js",
"nodemon.json"
],
"exec" : "node server.js",
"ext" : "js json jsx"
}