Last active
January 7, 2018 19:49
-
-
Save kevinchisholm/5e1726e83fba0bd63538b76ac31efa45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/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 })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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