Run through of setting up a basic node app with express
var express = require('express'); | |
// Middleware to parse body of HTTP requests | |
var bodyParser = require('body-parser'); | |
// Invokes function returned from require. | |
// Creates an Express application; | |
// express() returns Express object | |
var app = express(); | |
// Sets port equal to either the environment | |
// PORT variable OR 3000 as default. | |
// Can set up environment variables on servers | |
// when deploying for production. | |
var port = process.env.PORT || 3000; | |
// Create parser for requests with | |
// Content-Type: x-www-form-urlencoded | |
var urlencodedParser = bodyParser.urlencoded({ extended: false }); | |
// Create parser for requests with | |
// Content-Type: application/json | |
var jsonParser = bodyParser.json(); | |
// Mounts specified middleware function(s) at specified path; | |
// app.use() takes following params: | |
// 1. path: string|RegExp | (string|RegExp)[] | |
// 2. ...handlers: RequestHandler[] (callback functions, can be series of functions | |
// separated by commas or array of functions) | |
// Middleware function is executed when the base of | |
// the requested path matches path param. | |
// express.static() is built-in express middleware | |
// to serve static files. | |
// express.static() takes following params: | |
// 1. root: string (dir from which static assets to be served) | |
// 2. options?: serveStatic.ServeStaticOptions | |
// Here we have a 'public' folder in root dir with a style.css file | |
app.use('/assets', express.static(__dirname + '/public')); | |
// Make your own middleware. | |
app.use('/', function(req, res, next){ | |
console.log('Request Url: ' + req.url); | |
// Express provides next() for middleware to the | |
// function that you give as callback when route is matched. | |
// next() just means 'run the next middleware'; | |
// Without next(), request will be left hanging. | |
next(); | |
}); | |
// Executes this function if a GET request | |
// is received to this url. | |
app.get('/', function(req, res) { | |
// Indicate body to be included in response. | |
// Don't need to indicate Content-type header. | |
// When browser sees href=assets/style.css it will | |
// generate an HTTP req for site/assets/style.css, | |
// and this will be handled by Node. | |
// When it sees /assets it will handle request in | |
// app.use('/assets' ...). | |
res.send('<html><head><link href=assets/style.css type=text/css rel=stylesheet /></head><body><h1>Donuts</h1></body></html>'); | |
}); | |
// Use res.sendFile to serve an html file | |
app.get('/form', function(req, res){ | |
res.sendFile(__dirname + '/form.htm'); | |
}); | |
// :id indicates that id is a variable name. | |
// Access id using req.params.id. | |
// Can also access request query string using req.query.param, | |
// where param is name from a query string name value pair | |
// (ie url?param=4&name=value). | |
// Now we can access urls such as: | |
// /person/3 | |
// /person/Jerry | |
app.get('/person/:id', function(req, res) { | |
res.send('<html><head></head><body><h1>Donuts for Person ' + req.params.id + '</h1></body></html>'); | |
}); | |
// Pass urlencodedParser (middleware we created earlier) | |
// to post statement as a callback. | |
// urlencodedParser is middleware that will be called | |
// before function(req, res) is called. | |
// urlencodedParser gives us access to req.body.param, | |
// where param is name of name value pair in body of request. | |
// So if '/person' is seen in the url, and it's of method post, | |
// then this middleware and callback will be run. | |
app.post('/person', urlencodedParser, function(req, res){ | |
res.send('Thank you!'); | |
console.log(req.body.firstname); | |
console.log(req.body.lastname); | |
}); | |
// Pass jsonParser (middleware we created earlier) | |
// to post statement as a callback. | |
// jsonParser gives us access to req.body.param, | |
// where param is name in json object in body of request. | |
app.post('/personjson', jsonParser, function(req, res){ | |
res.send('Thankyou for the json data'); | |
console.log(req.body.firstname); | |
console.log(req.body.lastname); | |
}); | |
// Send json data in response to GET from /api url | |
app.get('/api', function(req, res) { | |
res.json({ firstname: 'Jelly', lastname: 'Donut' }); | |
}); | |
// listen() calls http.createServer under the hood. | |
// listen() takes the following params: | |
// 1. port: number | |
// 2. hostname: string | |
// 3. backlog: number | |
// 4. callback?: Function | |
// returns a http.Server object. | |
// Here we specify port 3000. | |
app.listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment