Welcome to FindNerd. Today we are going to discuss GET request in nodejs. There are different request methods available to process the data. Here we are going to take a small example on GET request in nodejs. First of all we want to mention that GET request is used to get the data or display the data. Kindly check our application structure below.
application folder: get_request_nodejs
Sub-folders and their files
package.json : set the dependencies
main.js: main application file
data folder: Includes index.js file to store the trains data in json format to display.
routes folder: Includes index.js file to perform the GET request.
train folder: It is a custom module which provides different functions to process the train information. It includes two files package.json and index.js.
Using above folders and their files we can process the GET request. Please follow the steps.
Step1: Create a folder named get_request_nodejs
Step2: Create a file package.json. Please check the script.
//package.json
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.0",
"jade": "*"
}
}
You can see in above code. we have set the dependencies such as express, jade frameworks. To install the dependencies execute the below command.
npm install
Step3: Create folder data and includes trains related data in json format inside index.js. Kindly check the data below.
module.exports = {
"54532": {
"type" : "passenger"
"number": 54532,
"origin": "UMA",
"destination": "BN",
"departs": "00:03",
"arrives": "09:00"
},
"70483": {
"type" : "express"
"number": 70483,
"origin": "GGO",
"destination": "MMA",
"departs": "13:00",
"arrives": "19:00"
}
};
You can add more data to test the process.
Step4: Now it is time to create module. Create folder named train. Create file package.son as well as index.js for modules functions.
//package.json
{
"name": "train",
"version": "0.0.0",
"description": "Train data management",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Deepak Verma <deepakverma.3008@gmail.com>",
"license": "BSD-2-Clause"
}
//index.js
var Train = function () {
this.data = {
type : null,
number: null,
origin: null,
destination: null,
departs: null,
arrives: null,
actualDepart: null,
actualArrive: null
};
this.store = function (info) {
for(var prop in this.data) {
if(this.data[prop] !== 'undefined') {
this.data[prop] = info[prop];
}
}
};
this.captureDepart = function () {
this.data.actualDepart = Date.now();
};
this.captureArrive = function () {
this.data.actualArrive = Date.now();
};
this.getTrainData = function () {
return this.data;
};
};
module.exports = function (info) {
var instance = new Train();
instance.store(info);
return instance;
};
In above module we have created four different functions such as store to pass the real data, captureDepart to capture real departure time, captureArrive to capture actual arrival time and getTrainData to display the data. We will use these functions in our application.
Step5: Create routes folder and It includes index.js. Please check the code below.
var trains = require('../data');
var train = require('../train');
for(var number in trains) {
trains[number] = train(trains[number]);
}
exports.train = function(req, res){
var number = req.param('number');
if (typeof trains[number] === 'undefined') {
res.status(404).json({status: 'error'});
} else {
res.json(trains[number].getTrainData());
}
};
Above code will process the GET request.
Step6: Create main.js. Kindly check the code below.
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/train/:number', routes.train);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
In first four lines we are loading modules such as express, routes, http and path. After that we are creating express object and calling function use and set the different values. We are requesting using GET method. We are passing train number in URL and getting other details accordingly.
Step7: Open terminal and go to application folder, run the below command
node main.js
Thank you for being with us!
0 Comment(s)