-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (54 loc) · 1.88 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
const express = require('express');
const session = require('express-session');
const app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
const bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
app.use(session({ secret: 'this-is-a-secret-token', cookie: { maxAge: 60000 }}));
// add other middleware
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function (request, response) {
request.session.category = 'gold';
request.session.views++;
request.session.dostuff = true;
response.render('pages/index', function (err, html) {
response.status(200).send(html);
});
});
app.post('/', function (request, response) {
let html = '<html><p>Posted to Express:</p>';
const body = request.body;
for (const key in body) {
if (body.hasOwnProperty(key)) {
html += `<p>${key}:${body[key]}</p>`;
}
}
html += '</html>\n';
response.status(200).send(html);
});
app.get('/json', function (request, response) {
response.status(200).json({president: 'lincoln'});
});
app.get('/send_array', function (request, response) {
response.status(200).send([1, 2, 3]);
});
app.get('/send_basic', function (request, response) {
response.status(200).send('whoop1');
});
app.get('/send_buffer', function (request, response) {
response.status(200).send(new Buffer('nail the door shut'));
});
app.get('/write', function (request, response) {
response.status(200);
response.type('text/plain');
response.write('hello');
response.write(' world');
response.end();
});
app.listen(app.get('port'), function () {
console.log('Node app is running on port', app.get('port'));
});