Node.js - app.get() route

January 10, 2020Stian Instebø
Snippets showing you how to easily manage get params in your node.js requests.

Node.js app routes is something that simplifies your API routes. Both for the app itself and for external API requests.

To get startet with get requests use the following code. This code will work after you've setup the necessary requires.

app.get("/page/:pageid/", function(req, res, next) {

	var startdate = req.params.start;
	var enddate = req.params.end;

	switch(req.params.pageid) {
		case '01':	
			//TODO: Show page 1
		break;

		case '02':
			//TODO: Show page 1
		break;

		default:
			//TODO: Handle error
		break;
	}

	res.send(xml);

});

Simple snippets explaining how to handle app.get routes in Node.js with params.

By adding ":param" to your get request, you can simply format your url's accordingly. and ex view different pages with different information depending on the user.

Our url's will look like this:
https://www.example.com/page/01/
https://www.example.com/page/02/

GET IN TOUCH WITH ME

All rights reserved © 2020 - Stian Wiik Instebø