고병후

Make router in server

1 +COOKIE_SECRET=cookiesecret
...\ No newline at end of file ...\ No newline at end of file
1 +{
2 + "id": "3"
3 +}
...\ No newline at end of file ...\ No newline at end of file
This diff is collapsed. Click to expand it.
1 +{
2 + "name": "tunnel",
3 + "version": "1.0.0",
4 + "description": "server of tunnel project",
5 + "main": "server.js",
6 + "dependencies": {
7 + "body-parser": "^1.19.0",
8 + "cookie-parser": "^1.4.6",
9 + "dotenv": "^10.0.0",
10 + "express": "^4.17.1",
11 + "express-session": "^1.17.2",
12 + "morgan": "^1.10.0"
13 + },
14 + "devDependencies": {
15 + "nodemon": "^2.0.15"
16 + },
17 + "scripts": {
18 + "test": "echo \"Error: no test specified\" && exit 1",
19 + "start": "node server.js"
20 + },
21 + "author": "amoroso",
22 + "license": "ISC"
23 +}
1 +const express = require("express");
2 +const path = require("path");
3 +const router = express.Router();
4 +
5 +router.get('/',(req,res)=>{
6 + res.sendFile(path.join(__dirname, '../../../turnel_FE/public/index.html'));
7 +})
8 +
9 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
1 +const express = require("express");
2 +const bodyParser = require("body-parser");
3 +const router = express.Router();
4 +
5 +router.get('/',(req,res)=>{
6 + res.send('로그인 페이지');
7 +});
8 +
9 +//라우트 매개변수사용
10 +//뒤에 위치 요구
11 +router.get("/:id",(req,res)=>{
12 + console.log(req.params, req.query);
13 + req.body;
14 +});
15 +
16 +
17 +
18 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
1 +const express = require("express");
2 +const bodyParser = require("body-parser");
3 +const router = express.Router();
4 +
5 +router.get('/',(req,res)=>{
6 + res.send('회원가입 페이지');
7 +});
8 +
9 +//라우트 매개변수사용
10 +//뒤에 위치 요구
11 +router.get("/:id",(req,res)=>{
12 + console.log(req.params, req.query);
13 + req.body;
14 +});
15 +
16 +
17 +
18 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
1 -var express = require('express'); 1 +const express = require("express");
2 -var app = express(); 2 +const path = require("path");
3 -var bodyParser = require('body-parser'); 3 +const morgan = require("morgan");
4 -var session = require('express-session') 4 +const cookieParser = require("cookie-parser");
5 +const session = require("express-session");
6 +const dotenv = require("dotenv");
7 +const bodyParser = require("body-parser");
8 +
9 +dotenv.config();
10 +const indexRouter = require('./routes/index.js');
11 +const userRouter = require('./routes/login.js');
12 +const registerRouter = require('./routes/regiser.js');
13 +
14 +const app = express();
15 +app.set('port', process.env.PORT || 3000);
16 +const port = app.get('port');
17 +
18 +//미들웨어 설정
19 +app.use(morgan('dev'));
20 +app.use(express.json()); //json형식으로 데이터 전달
21 +app.use(express.urlencoded({extende: false})); // url형식으로 형식으로 데이터 전달
22 +app.use(cookieParser(process.env.COOKIE_SECRET));
23 +app.use(session({
24 + resave: false,
25 + saveUninitialized: false,
26 + secret: 'keyboard cat' ,//process.env.COOKIE_SECRET,
27 + cookie: {
28 + httpOnly: true,
29 + secure:false,
30 + },
31 + name: 'session-cookie',
32 +}));
33 +
34 +//라우팅
35 +app.use('/',indexRouter);
36 +app.use('/login',userRouter);
37 +app.use('/register',registerRouter);
38 +
39 +
40 +//에러처리 미들웨어
41 +//존재하지 않는 경로가 들어오면 오류 응답
42 +app.use((req,res,next)=>{
43 + console.log("유저가 존재하지 않은 경로 접근");
44 + res.status(404).send("NOT FOUND");
5 45
6 -app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }})) 46 +});
47 +
48 +//포트를 연다.
49 +app.listen(port, ()=>{
50 + console.log(port,"번 포트로 대기중");
51 +});
52 +
53 +//라우팅
54 +
55 +/*
56 +//루트페이지
57 +app.get('/', (req,res)=>{
58 + res.sendFile(path.join(__dirname, '/fe/index.html'));
59 +});
60 +//로그인페이지
61 +app.get('/login', (req,res)=>{
62 + res.send("로그인페이지");
63 +});
64 +
65 +app.get('/register', (req,res)=>{
66 + res.send("회원가입 페이지");
67 +});
68 +
69 +*/
70 +
71 +
72 +
73 +
74 +
75 +/* 물어볼 부분
76 +app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}));
7 app.use(bodyParser.urlencoded({ extended: false })); 77 app.use(bodyParser.urlencoded({ extended: false }));
8 app.use(bodyParser.json()); 78 app.use(bodyParser.json());
79 +*/
9 80
81 +/*
10 var users = new Array(); 82 var users = new Array();
11 users[0] = { 83 users[0] = {
12 "userId" : 1, 84 "userId" : 1,
...@@ -147,5 +219,6 @@ app.delete('/users/:userId',auth,function(req, res){ ...@@ -147,5 +219,6 @@ app.delete('/users/:userId',auth,function(req, res){
147 res.send("Not Found"); 219 res.send("Not Found");
148 } 220 }
149 }); 221 });
222 +*/
150 223
151 -var server = app.listen(80);
...\ No newline at end of file ...\ No newline at end of file
224 +//var server = app.listen(80);
...\ No newline at end of file ...\ No newline at end of file
......