-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
80 lines (63 loc) · 2.91 KB
/
index.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
// CommonJS format for importing packages.
// var express = require('express');
// var bodyParser = require('body-parser');
// var cookieParser = require('cookie-parser');
// ES6 format for importing modules
import express from 'express';
// Since body-parser is CommonJS module and not ES6 module,
// it can always be imported via the default export, for example using:
// $ import pkg from 'body-parser';
// $ const { urlencoded, json } = pkg;
// otherwise for using:
// $ import { urlencoded, json } from 'body-parser';
// Syntax Error is thrown
import pkg from 'body-parser';
const { urlencoded, json } = pkg;
// ES6 format for importing modules
import cookieParser from 'cookie-parser';
// above ES6 format gives syntax error:
// Cannot use import statement outside a module
// the solution is to add: {"type": "module"} in package.json
// but this is also giving error because, node version is 12
// ES6 imports are allowed in node version 13 or greater.
// So updating to node 13 or greater, which is 14.15.4 LTS
var app = express();
// using bodyParser populates the body of the request, otherwise body is unidentified
// app.use(bodyParser()); // Deprecated
// using cookieParser populates the cookies of the request, req.cookies, otherwise cookie is unidentified
app.use(cookieParser());
// since the above constructor is deprecated, bodyParser(),
// the below commands are required to run seperately
app.use(urlencoded({extended: true}));
app.use(json());
// require router defined in and exported from movies.js
// in other words, importing the router
import movies_router from './movies.js';
// use the router on the sub-route /movies
// eg: when client a sends a request using, localhost:3000/movies,
// the request will be re-routed to ./movies.js file which is referenced
// by movies variable
app.use('/movies', movies_router);
// when client a sends a GET request using, localhost:3000/
// below handler will run
app.get('/', function(req, res){
// req.query can be used inside this GET handler, req.body can't be used
console.log('Welcome to index.js using GET request');
res.status(200).send('Welcome to index.js using GET request');
});
// note: req.query or req.body is a JSON object with key-value pairs
// and can be accessed using: value = req.body['key']
// when client a sends a POST request using, localhost:3000/
// below handler will run
app.post('/', function(req, res){
// req.body can be used inside this POST handler, req.query can't be used
console.log('Welcome to index.js using POST request');
res.status(200).send('Welcome to index.js using POST request');
});
// the node app listens for client requests on port 3000
app.listen(3000);
console.log('server listening on port 3000');
// for a better insight what above commands/keywords mean, you can visit:
// https://www.javatpoint.com/expressjs-tutorial
// for RESTful APIs:
// https://www.tutorialspoint.com/expressjs/expressjs_restful_apis.htm