-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
40 lines (31 loc) · 1.17 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
//require the express nodejs module
var express = require('express'),
//set an instance of exress
app = express(),
//require the body-parser nodejs module
bodyParser = require('body-parser'),
//require the path nodejs module
path = require("path");
//support parsing of application/json type post data
app.use(bodyParser.json());
//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: true }));
//tell express that www is the root of our public web folder
app.use(express.static(path.join(__dirname, 'www')));
//tell express what to do when the /form route is requested
app.post('/form',function(req, res){
res.setHeader('Content-Type', 'application/json');
//mimic a slow network connection
setTimeout(function(){
res.send(JSON.stringify({
firstName: req.body.firstName || null,
lastName: req.body.lastName || null
}));
}, 1000)
//debugging output for the terminal
console.log('you posted: First Name: ' + req.body.firstName + ', Last Name: ' + req.body.lastName);
});
//wait for a connection
app.listen(3000, function () {
console.log('Server is running. Point your browser to: http://localhost:3000');
});