Skip to content

Instantly share code, notes, and snippets.

@kevinchisholm
Last active January 7, 2018 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinchisholm/5e1726e83fba0bd63538b76ac31efa45 to your computer and use it in GitHub Desktop.
Save kevinchisholm/5e1726e83fba0bd63538b76ac31efa45 to your computer and use it in GitHub Desktop.
//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 /about 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');
});
{
"dependencies": {
"body-parser": "^1.15.2",
"express": "4.14.0"
},
"scripts": {
"start": "node server.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment