NodeJS and Express Basics
This article is about how-to setup a NodeJS server and define routes using Express Framework.
NodeJS is a free open-source server environment and this allows to run JavaScript on the server. Express (or Express JS) is a JavaScript framework which makes easy to write server-side JavaScript code. It also provides a lot of other features to develop a robust server-side application using NodeJS.
Prerequisites
To setup and run a NodeJS application, following are the few prerequisites:
- NodeJS
- Node Package Manager (NPM)
By running below commands in terminal (or Command Prompt), this can be verified whether Node and NPM are already installed in the system or not:
After running both the commands, it should print node and npm versions as shown below:
If not installed, please download NodeJS and follow the instructions mentioned in the site:
Project setup and installation
Create a folder for your project. Open Visual Studio Code (or any editor). Open terminal (or cmd in Windows) and navigate to the project folder.
Create package.json
A package.json file is a JSON (not JSON object in a JS file) file which contains required metadata about the application. It also contains the list of dependencies to be installed and used in the project using npm. Run below command in terminal to create package.json:
This command will ask for inputs for each property of the package.json. Please provide value or press enter to keep the default values unchanged.
Below is a sample package.json structure:
To create package.json in one go with all the default values, run below command:
Install express
Run below command to download express package to your system:
Note: sudo is used to install the package with admin rights. This might not require in many cases.
After running above command, package will be included in the project folder and express will in included in the dev-dependencies section in package.json.
Let’s do some coding
In package.json file, “index.js” is mentioned as the value of “main” property in the JSON. So, index.js is the main entry file for the project. So, a file with name “index.js” needs to be created. After creating index.js, add following code to the file:
In the above code:
- express package is imported and assigned to a variable express
- A new instance of express is created and assigned to variable app
- port is defined as 3000 for the server. This could be any number.
- A route is defined as “/”, which will be the default path for the site. Once the default page is loaded in the browser, it should print ‘Hello World!’.
- Last line will start the Node server.
Run below command (any one) in terminal to start the server:
On successful running of the server, following statement should be printed in the terminal:
Example app listening on port 3000
Express generator
Application generator tool, express-generator npm package, can be used to scaffold a new application quickly. This will create a pre-defined folder structure with several JS and other configuration files. For more details, refer:
Reference:
https://expressjs.com/en/starter/hello-world.html