Contact Us

Getting Started with Express JS

Learn how to build your "Hello World" Express JS App

getting started with express
Brief Introduction to Express JS
ExpressJS is bare-bones web application framework on top of NodeJS. It aims to be simple, fast and robust framework for writing web-applications, resful APIs and hybrid Apps. It is simple but provides a great solution for creating projects of any complexity.
In this tutorial we'll see how to get up and running with ExpressJS by creating a simple "Hello World of Express" app.
Getting Express
Execute the following command in terminal or cmd to get ExpressJS installed on your system globally .

	npm install express -g 
Create your First App
Create a file named helloworld.js and enter the following code into it.

var express = require('express');

var app = express();

	res.send('Hello World!');


	console.log('App listening on port 3003!');
Running the App Locally
In order to test your app, you can run it locally by executing the following command in the same directory as the code file.

	node helloworld.js
Open App in Browser
In order to see your app in working state, just direct your browser to http://localhost:3003
You should see a page showing the text Hello World
Serve Static Content
Right now our app looks dull & boring . So let's add some static html. For this we need to do 2 things.
  1. Edit helloworld.js to add code to serve static files
  2. Setup a directory to store the static content

Step-1 Edit helloworld.js

Simply open helloworld.js file and add the following line of code to it.

app.use('/public', express.static(__dirname + '/public'));

Step-2.1 Create static content directory

	mkdir public

Step-2.2 Add static html file to public directory

Create a file named simple.html in your public directory and add following markup to it.

	<!DOCTYPE html>
	<title> Express JS </title>
		body {
			padding: 50px;
			background: #2f2f2f;
			text-align: center;
		h1 {
			color: #f2f2f2;
			font-size: 42px;
		h2 {
			color: #e2e2e2;
			font-size: 28px;
		a {
			color: #f2f2f2;
			font-size: 18px
	<h1> Simple Express JS App </h1>
	<h2> Fast, unopinionated, minimalist web framework for Node.js </h2>
	<a href="https://rishabh.io/recipes/getting-started-with-express.html">  Express Tutorial	</a> 


After adding above file, run your helloworld.js file again and see the results in browser by directing your browser to http://localhost:3003/public/simple.html
It should render a beautiful static html page in your broswer as shown below.

serving static content with express
Similarly your can serve other static content like image files, css files, js files etc.
Add Routes for other HTTP Methods
Our app looks a little better now, so let's add to it's intelligence by adding routes for other http methods like put, post and delete in our app. Just open your helloworld.js file and add the following code to it.

app.post('/post', function (req, res) {
  res.send('POST Request Working File!');

app.put('/put', function (req, res) {
  res.send('Got a PUT HTTP request! Wanna Edit Something? ');
We can again restart our app and see the above routes working by using a tool like postman and making post and put requests to the following urls:
  1. http://localhost:3003/post
  2. http://localhost:3003/put
Similarly we can keep on adding more routes to our app as and when needed.
In this tutorial, we learned to create a basic ExpressJS app. We also learned to serve static content and added basic routing to our app. We should now be in a position to appreciate the power and flexibility which ExpressJS brings to creating WebApps and We should be ready to explore more complex operations and techniques offered by ExpressJS.
Where to Go! next

More from RISHABH.IO

developed & nourished with by rishabh.io