Create Server Project
Create LearnExpressJSRestAPIWithRealApps folder and select to this folder in Visual Studio Code
Install Express.JS
Use the following command to install Express.JS:
npm install express --save
npm install body-parser --save
npm install cookie-parser --save
npm install multer --save
Create Rest API
Create a new folder named api inside the server project. Create demo.api.js file inside api folder contains Rest APIs provides text/plain data for the client
var DemoAPI = {
hello: function (request, response) {
response.setHeader('Content-Type', 'text/plain');
response.send('Hello ' + request.params.fullName);
},
sum: function (request, response) {
response.setHeader('Content-Type', 'text/plain');
var result = parseInt(request.params.a) + parseInt(request.params.b);
response.send(result.toString());
}
};
module.exports = DemoAPI;
Create Rest API Routing
Inside the api folder create a new file named index.js. This file will hold all the routes needed for rest api in server.
var express = require('express');
var router = express.Router();
var DemoAPI = require('./demo.api');
router.get('/demo/hello/:fullName', DemoAPI.hello);
router.get('/demo/sum/:a/:b', DemoAPI.sum);
module.exports = router;
Create Rest API Server
At the root of server project, create a file named server.js. This will be the entry point into node application. This will start the server and listen on a local port
var express = require('express');
var app = express();
app.use('/api', require('./api/index'));
var server = app.listen(9090, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Server listening at http://%s:%s", host, port)
});
Structure of Server Project
Test Rest API Server
At the root of server project run command: node server.js
Access Rest API use the following url: http://localhost:9090/api/demo/hello/Kevin
Output
Hi Kevin
Access Rest API use the following url: http://localhost:9090/api/demo/sum/1/2
Output
3
Create Client Project
Create LearnExpressJSRestAPIWithRealApps_Client folder and select to this folder in Visual Studio Code
Install Modules
Use the following command to install http module
npm install http --save
Create Demo Rest API Client
Create a new folder named api inside the client folder. Create demo.api.js file inside api folder contains method call Rest API from server
var http = require('http');
var BASE_URL = 'http://localhost:9090/api/';
var DemoAPIClient = {
hello: function (fullName, callback) {
return http.get(BASE_URL + 'demo/hello/' + fullName, function (response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
callback({
status: response.statusCode,
body: str
});
});
response.on('error', function (error) {
callback({
status: response.statusCode,
error: error.message
});
});
}).end();
},
sum: function (a, b, callback) {
return http.get(BASE_URL + 'demo/sum/' + a + '/' + b, function (response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
callback({
status: response.statusCode,
body: str
});
});
response.on('error', function (error) {
callback({
status: response.statusCode,
error: error.message
});
});
}).end();
}
};
module.exports = DemoAPIClient;
Structure of Client Project
Test Client Project
At the root of client project, create a file named client.js. This will call Rest API and display results
var DemoAPIClient = require('./api/demo.api');
DemoAPIClient.hello('Kevin', function (result) {
if (result.error) {
console.log(result.error)
} else {
console.log('Status: ' + result.status);
console.log('Body: ' + result.body);
}
});
DemoAPIClient.sum(1, 2, function (result) {
if (result.error) {
console.log(result.error)
} else {
console.log('Status: ' + result.status);
console.log('Body: ' + result.body);
}
});
Output
Status: 200
Body: Hello Kevin
Status: 200
Body: 3