고병후

Make router in server

COOKIE_SECRET=cookiesecret
\ No newline at end of file
{
"id": "3"
}
\ No newline at end of file
This diff is collapsed. Click to expand it.
{
"name": "tunnel",
"version": "1.0.0",
"description": "server of tunnel project",
"main": "server.js",
"dependencies": {
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.6",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express-session": "^1.17.2",
"morgan": "^1.10.0"
},
"devDependencies": {
"nodemon": "^2.0.15"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "amoroso",
"license": "ISC"
}
const express = require("express");
const path = require("path");
const router = express.Router();
router.get('/',(req,res)=>{
res.sendFile(path.join(__dirname, '../../../turnel_FE/public/index.html'));
})
module.exports = router;
\ No newline at end of file
const express = require("express");
const bodyParser = require("body-parser");
const router = express.Router();
router.get('/',(req,res)=>{
res.send('로그인 페이지');
});
//라우트 매개변수사용
//뒤에 위치 요구
router.get("/:id",(req,res)=>{
console.log(req.params, req.query);
req.body;
});
module.exports = router;
\ No newline at end of file
const express = require("express");
const bodyParser = require("body-parser");
const router = express.Router();
router.get('/',(req,res)=>{
res.send('회원가입 페이지');
});
//라우트 매개변수사용
//뒤에 위치 요구
router.get("/:id",(req,res)=>{
console.log(req.params, req.query);
req.body;
});
module.exports = router;
\ No newline at end of file
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var session = require('express-session')
const express = require("express");
const path = require("path");
const morgan = require("morgan");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const dotenv = require("dotenv");
const bodyParser = require("body-parser");
dotenv.config();
const indexRouter = require('./routes/index.js');
const userRouter = require('./routes/login.js');
const registerRouter = require('./routes/regiser.js');
const app = express();
app.set('port', process.env.PORT || 3000);
const port = app.get('port');
//미들웨어 설정
app.use(morgan('dev'));
app.use(express.json()); //json형식으로 데이터 전달
app.use(express.urlencoded({extende: false})); // url형식으로 형식으로 데이터 전달
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use(session({
resave: false,
saveUninitialized: false,
secret: 'keyboard cat' ,//process.env.COOKIE_SECRET,
cookie: {
httpOnly: true,
secure:false,
},
name: 'session-cookie',
}));
//라우팅
app.use('/',indexRouter);
app.use('/login',userRouter);
app.use('/register',registerRouter);
//에러처리 미들웨어
//존재하지 않는 경로가 들어오면 오류 응답
app.use((req,res,next)=>{
console.log("유저가 존재하지 않은 경로 접근");
res.status(404).send("NOT FOUND");
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
});
//포트를 연다.
app.listen(port, ()=>{
console.log(port,"번 포트로 대기중");
});
//라우팅
/*
//루트페이지
app.get('/', (req,res)=>{
res.sendFile(path.join(__dirname, '/fe/index.html'));
});
//로그인페이지
app.get('/login', (req,res)=>{
res.send("로그인페이지");
});
app.get('/register', (req,res)=>{
res.send("회원가입 페이지");
});
*/
/* 물어볼 부분
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
*/
/*
var users = new Array();
users[0] = {
"userId" : 1,
......@@ -147,5 +219,6 @@ app.delete('/users/:userId',auth,function(req, res){
res.send("Not Found");
}
});
*/
var server = app.listen(80);
\ No newline at end of file
//var server = app.listen(80);
\ No newline at end of file
......