In this post I will show the steps to create a very simple Express web application in NodeJS.
First Steps
After installing NodeJS in your system, create a new directory where the application files will reside, then switch to this new directory :
We now use the Node Package Manager (npm) to install the Express framework.
Note: npm is automatically installed with NodeJS (select the LTS version of the NodeJS download).
First, use the npm init command to create a package.json file. This file will contain a list of all dependencies required by the project (if you are familiar with Maven in Java, this corresponds to the pom.xml file). In our case, the only dependency will be the Express package:
When running this command, you can press RETURN to accept the default values when prompted. Make note of the “entry point:” option, the default value is index.js, this will be the name of the main application file.
Now, install Express using the npm install command, and use the --save option to automatically save it to the dependencies list (inside the package.json file):
Creating the App
Now we are ready to start writing our simple web app. We are going to create another directory named views under our current working directory. This will contain a static HTML file that will be served by our app when a HTTP GET request is made to the root route (/).
The index.html file is a simple HTML document with an <h1> tag:
Then create the index.js file inside your working directory. Open the file in your favorite editor and type the following code:
In lines 1 and 2 above, we load the Express module and create an instance named app. Line 3 declares a port variable, this will be the port on which the app will listen for requests. If environment variable PORT is empty, then the default port 3000 is used instead. The use() method in line 5 allows us to serve static content located in the /views directory. In line 7, the get() method handles HTTP GET requests. The first argument (“/”) is the end point to which the request is made, and the second argument is the handler function called to serve the request. The req and res arguments to the handler function are objects that wrap NodeJS’ HTTP request and response objects. They are used to extract request information and set response data respectively (we can name these arguments anything we want, but req and res are used conventionally). In line 8 we use the send() method of the res object, this method sends the HTTP response. In our app, we are sending the index.html file as a response to the GET request. There are many other methods available from the response object, they can be found on the Express API documentation. Finally, in line 11 we use the listen() method to tell our app to start listening to requests on the specified port.
Running the App
To run the app, on your working directory enter command node index.js. This will start the application and begin listening for requests.
A message like this should be returned:
Now open a web browser and go to http://localhost:3000. Our simple HTML file is returned:
Conclusion
This is one of the most basic apps we can create with Express and NodeJS, but it provides a good example of how easy it is to get started. Express provides many more features to build more complex applications. Hopefully, this post helped introducing the basics of Express.