I am learning development on MEAN stack and will share the series of blogs which will hopefully help others.
Explain the project
I am developing a e-commerce solution for medical retail store which will be having following features:- 1) Product ordering
2) Product inventory update on delivery received. 3) Dashboard for store admin to see the stock inventory, alerts
for expiring products.
Technology used
- Node - package manager & web server
- Express framework - web framework to write Restful API
- Mongo DB - Non relational database for persistence of data. Here we will be using cloud hosted mongo db
- Ionic framework - web application framework to develop web application and hybrid mobile application
- Heroku - cloud platform for hosting the application and API.
- VS Code - IDE to develop application
- Github - Source code repository to store code base.
Part 1 : Develop a Restful API using Nodejs using express framework
Let's start cracking with API development.
Setting up the environment
- Install Node js from https://nodejs.org
- Create a project folder
- Open console on the project folder
- Run command npm init, this will create a package.json file for further package tracking
- Install express framework with command npm install express --save. --save command will help in persisting the
reference. This way I don't need to upload the packages on source control repository and it will be downloaded
anywhere by npm install command.
Create a server.js file on folder and write the below code:
var express = require("express");
var app = express();
app.set('port', (process.env.PORT || 5000));
var app = express();
app.set('port', (process.env.PORT || 5000));
the server.
//Setting up a server
var server= app.listen(app.get('port'), function() {
var port= server.address().port;
console.log("App is running on port",port);
var port= server.address().port;
console.log("App is running on port",port);
});
Let's test if the application is running successfully by command npm start. You should
see the message logged "App is running on port". Let's write our first routes
app.get('/', function (req, res) {
res.send('welcome to my API!');
});
On npm start command, we should see a page with welcome to my API message.
Now let's write our API end point. This is simple Get method for products API.
var productRouter = express.Router();
productRouter.route('/api/products')
.get(function (req, res) {
Product.find(function (err, products) {
if (err) {
res.status(500).send(err);
console.log(err);
}
else { res.json('List of products'); }
});
});
entity framework in dotnet world. Let's install mongoose by running npm install mongoose --save.
I have used mongodb cloud instance in this case on https://cloud.mongodb.com
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
//Remember to change this
var db = mongoose.connect('mongodb://localhost:27017/medicineAPI');
//Schema definition
var productModel = new Schema({
skuid: { type: String },
name: { type: String },
category: { type: String }
});
module.exports = mongoose.model('Product', productModel);
Let's change the API routing code to below.
productRouter.route('/products').get(function(req, res) {
Product.find(function (err, products) {
if (err) {
res.status(500).send(err);
console.log(err);
}
else {res.json(products);}
});
});
Let's add a POST method. To add this we need another plug in called body-parser to understand JSON body. The response status
in this case is 201 and it returns the saved object.
var bodyparser = require('body-parser');
// Body parser middleware
app.use(bodyparser.urlencoded({ extended: true }));
app.use(bodyparser.json());
productRouter.route('/products')
.post(function (req, res) {
var products =new Product(req.body);
products.save();
res.status(201).send(products);
})
Now let's test the methods by using postman (a chrome plugin), you can use any other familiar tool as well.
URL: localhost:5000/api/products
POST Input json :-
Our first API is ready with POST and GET methods. The final code is available at https://github.com/manibs/StoreManage
The next part will follow up which will contain deployment of this API to cloud and build web application.
Comments
Post a Comment
Thanks for your valuable comment