articles

Home / DeveloperSection / Articles / Create Express Project in Node.js

Create Express Project in Node.js

Anchal Kesharwani 9211 18-Aug-2014

In this article, I’m explaining how to install express in node.js, how to create an

express project in node.js.

If you want to learn how to install and setup node.js first, then you can read my

previous article using below link:

Install and Setup Node.js

First open your command prompt with Administrator.

Step 1 Install express

Now that we have Node running, we need the rest of the stuff we're going to actually use to create a working website. To do that we're going to install Express, which is a framework that takes Node from a barebones application and turns it into something that behaves more like the web servers we're all used to working with (and actually quite a bit more than that). We need to start with Express, because we're going to use its scaffolding to get the rest of what we need (more on that in a second). So let's type this:

Create Express Project in Node.js

This installs some core Express functionality right into our Node installation, making it available globally so we can use it anywhere we want. That's handy. You'll see a bunch of text in your command prompt, mostly a lot of http 304's and GETs. That's fine. Express is now installed and available. 

After express installation, you need to express generator as well. This is following a trend in the node industry of breaking out core functionality from site-scaffolding utility (check out Yeoman sometime if you want to learn a whole lot more about scaffolding options out there). Installing the generator is very easy: in the same console you used to install express globally, type the following:

Create Express Project in Node.js

The generator should auto install, and since it (like all packages installed with -g) lives in your master NPM installation directory, it should already be available in your system path. So let's use our generator to create the scaffolding for a website.

Step 2 Create a new project

After the installation we are ready to create a new project. So let we are creating a simple project for understanding the express nodejs.
First of in the command prompt you change the location and where you want to keep your project. Here, I select D:\node and type the ‘express project name‘. 

Create Express Project in Node.js 

Hit enter and watch it go. You'll see something like this: 

Create Express Project in Node.js 

Step 3 Edit Dependencies

Here, the some basic structure for express installation runtime created. You will open the pachage.json file in nodejstest project folder. 

{  "name": "nodejstest",
}  
   "version": "0.0.1",
   "private": true,
   "scripts": {
        "start": "node ./bin/www"
                },
   "dependencies": {
   "express": "~4.2.0",
   "static-favicon": "~1.0.0",
   "morgan": "~1.0.0",
   "cookie-parser": "~1.0.1",
   "body-parser": "~1.0.0",
 
   "debug": "~0.7.4",
   "jade": "~1.3.0"
                }     

This is a basic JSON file describing our app and its dependencies. We need to add a few things to it. Specifically, calls for MongoDB and Monk. Let's make our dependencies object look like this:

{

  "name": "nodejstest",
   "version": "0.0.1",
   "private": true,
   "scripts": {
        "start": "node ./bin/www"
                },
   "dependencies": {
   "express": "~4.2.0",
   "static-favicon": "~1.0.0",
   "morgan": "~1.0.0",
   "cookie-parser": "~1.0.1",
   "body-parser": "~1.0.0",
 
   "debug": "~0.7.4",
   "jade": "~1.3.0",
    “mongodb”: “*”,
    “mongo”: “*”
                }
}    

Step 4 Install Dependencies

Note that the asterisks tells the latest version of dependencies on NPM. Now we will install the dependencies.

Create Express Project in Node.js

Now we want are prepared to run but we create data for setting up data for later.

Create Express Project in Node.js

That's where we're eventually going to store our MongoDB data. If that directory doesn't exist, the database server will choke when we go to run it later. We're not doing anything with it right now, though, so let's test out our web server! Type the following:

Create Express Project in Node.js

Everything working? Awesome! Open a browser and head for http://localhost:3000 where you will see a welcome to express page.

Create Express Project in Node.js 

I hope that this article was helpful for you. Thanks!

 


Updated 07-Sep-2019

Leave Comment

Comments

Liked By