ExpressJS Snippets

1)
If next is called then it function will not return after execution of request.

app.get(/dishes/:dishId, function(req, res, next) {
res.end(‘sending it to server’+ req.params.dishId);
});

2)
Get the request data
req.body.name; req.body.description

3)
res.write();
res.end();

4) Express Routers

var dishRouter = express.Router();dishRouter.use(bodyParser.json());

dishRouter.route(‘/’)
.all(function(req,res,next){
res.writeHead(200, {‘Content-type’:’text/plain’});
next();
})
.get(function(req,res, next) {
res.end(‘retrieving the request’);
});

app.use(‘/dishes’, dishRouter);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s