ExpressJS
- Is a web application framework for Nodejs
- Discharged as free and open source computer program under the MIT Permit
- Is outlined for building web applications and APIs
- Provides a robust set of features for web and mobile applications
- Developed ans maintained by the Node js foundation
- High performance, extensible and reusable
How to start developing and using the Express Framework.
- You should have the Node and the npm (node package manager) installed.
- Confirm that node and npm are installed by running the following commands in terminal.
node --version
npm -- version
- Start your terminal/cmd, create a new folder
- Now to create the package.json file using npm
npm init
- To install Express and add it to our package.json file, use the following command
npm install --save express
- To make our development process a lot easier, we will install a tool from npm, nodemon. This tool restarts our server as soon as we make a change in any of our files
npm install -g nodemon
Simple project using Express js
Print Hello world using Expressjs
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send("Hello world!");
});
app.listen(3000);
Save the file, go to your terminal and type the following.
nodemon index.js
ExpressJS – Best Practices :
- Always begin a node project using npm init.
- Always install dependencies with a –save or –save-dev. This will ensure that if you move to a different platform, you can just run npm install to install all dependencies.
- Stick with lowercase file names and camelCase variables. If you look at any npm module, its named in lowercase and separated with dashes. Whenever you require these modules, use camelCase.
- Don’t push node_modules to your repositories. Instead npm installs everything on development machines.
- Use a config file to store variables
- Group and isolate routes to their own file. For example, take the CRUD operations in the movies example we saw in the REST API page.
Nice work
ReplyDeleteThank you.
Delete