-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-server.js
More file actions
54 lines (38 loc) · 1.46 KB
/
node-server.js
File metadata and controls
54 lines (38 loc) · 1.46 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const PORT = process.env.PORT || 8080;
/* Creating express instance and assigning to app */
const app = express();
/* Here we are setting port to express server on which server will listen*/
app.set('port', PORT);
/* We are parsing request object using bodyParser or express */
app.use(bodyParser.json());
app.use(express.json());
/* serving static file on server */
app.use(express.static(path.join(__dirname + '/dist')));
/* Middleware for express which can access of request, response and next
* Using middleware we can check authentication and authorization
* It will execute between request and response
* */
app.use((req, res, next) => {
/*Heare we are attaching header to response */
res.setHeader('Content-Type', 'text/html');
res.setHeader('status', 201);
/* We can attach header on response using write also */
res.write('status', 202);
// call for authentication check
next();
});
app.use('/', (req, res) => {
/* This we are using for send the file whith response
* we can send html, text or json files
* */
res.sendFile(path.join(__dirname + '/dist/index.html'));
/* for sending any text over response*/
res.send('Hello world !');
});
/* Creating Https server which will listen on port using listen*/
app.listen(app.get('port'), () => {
console.log('Server is running on port ' + app.get('port'));
});