-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
29 lines (21 loc) · 1.01 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
const http = require('http');
const database = require('./database');
const usersController = require('./controller/users-controller');
const nationalParksController = require('./controller/national-parks-controller');
const port = 3000;
const message = `Server listens at http://localhost:${port}`;
database('safari');
http
.createServer((req, res) => {
// setting up headers
res.writeHead(200, { "Content-Type": "application/json", 'Access-Control-Allow-Origin': '*' });
// users route
if (req.url === '/api/v1/users' && req.method == 'GET') usersController.index(req, res);
// creates national parks
else if (req.url === '/api/v1/national-park' && req.method == 'POST') nationalParksController.create(req, res);
// gets all national parks
else if (req.url === '/api/v1/national-parks' && req.method == 'GET') nationalParksController.index(req, res);
else if (req.url === '/') res.end(' You are home');
else res.end('not found');
})
.listen(port, () => console.log(message));