-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
107 lines (70 loc) · 2.4 KB
/
server.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const express = require('express');
const dotenv = require('dotenv')
const morgan = require('morgan')
const connectDB = require('./config/db')
dotenv.config({path: './config/config.env'})
const bootcamps = require('./routes/bootcamps')
// Connect to database
connectDB();
const app = express();
// Body parser
app.use(express.json())
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}
app.use('/api/v1/bootcamps', bootcamps);
const PORT = process.env.PORT || 9000;
const server = app.listen(
PORT, console.log(`Serving is running in ${process.env.NODE_ENV} mode on port ${PORT}`)
)
// const http = require('http');
// const todos = [
// {id: 1, text: 'Todo one'},
// {id: 2, text: 'Todo two'},
// {id: 3, text: 'Todo three'}
// ]
// const server = http.createServer ((req, res) => {
// // res.setHeader('Content-Type', 'text/plain')
// // res.setHeader('Content-Type', 'application/json')
// // res.setHeader('X-Powered-By', 'Node.js')
// const {method, url} = req;
// let body = [];
// req.on('data', chunk => {
// body.push(chunk);
// }).on('end', () => {
// body = Buffer.concat(body).toString()
// let status = 404;
// const response = {
// success: false,
// data: null
// }
// if (method === 'GET' && url === '/todos') {
// status = 200;
// response.success = true;
// response.data = todos;
// } else if(method === 'POST' && url === '/todos') {
// const { id, text} = JSON.parse(body)
// todos.push({ id, text});
// status = 200;
// response.success = true;
// response.data = todos;
// if(!id || !text) {
// status = 400;
// response.error = 'please add an id and text'
// } else {
// status = 200;
// response.success = true;
// response.data = todos;
// }
// }
// res.writeHead(status, {
// 'Content-Type': 'application/json',
// 'X-Powered-By': 'Node.js'
// });
// console.log(req.headers.authorization)
// res.end(JSON.stringify(response)
// );
// });
// });
// PORT = 10000;
// server.listen(PORT, () => console.log(`server is running on port ${PORT}` ));