Create a basic express application

To create a basic express application, the only thing needed is adding a dependency to express in the package.json manifest and run npm install.
A dependency to jade is added, as jade is the view engine of choise for my basic express application.

No need to add the node_modules directory to GIT as they are all installed by using npm install. Therefor, the modules need to be added as a dependency in the package.json manifest.

    "dependencies": {
        "express": "*",
        "jade": "*"
    }

"*" means we're using the latest versions, a version can be selected, for example

    "dependencies": {
        "express": "4.9.0"
    }

The current version of express can be found by typing "express -V" in command 
line.

The package.json manifest


{
    "name": "express-basic",
    "version": "0.0.1",
    "private": "true",
    "dependencies": {
        "express": "*",
        "jade": "*"
    }
}

Now, run npm install so that the latest version of express and jade can be installed.

App.js file


// Load http module.
var http = require('http');
// Load express module.
var express = require('express');

// Initialize app object.
var app = new express();

// Basic routing.
app.get('/', function(req, res) {
    res.send('Front page');
});

// Create server and listen on port 3030.
http.createServer(app).listen(3030, function() {
    console.log('Server running...');
});

Start the app by typing app.js in the command line.

See the difference with a node server without using express

At this time, we have done nothing with the view engine jade, so lets start using it. I've created a directory views in the root of the project and added an index.jade file inside the directory.


html
    head
        title Index page using Jade
    body Front page

The app.js file needs some changes in order to load the index.jade file.


// Load http module.
var http = require('http');
// Load express module.
var express = require('express');

// Initialize app object.
var app = new express();

// Use app.set to add the view engine.
// Ass app is an express object, it has a view engine property.
app.set('view engine', 'jade');
 
// Set path to views.
app.set('views', './views');

// Basic routing.
app.get('/', function(req, res) {
    // res.send is changed to result.render in order to load the correct view.
    res.render('index');
});

// Create server and listen on port 3030.
http.createServer(app).listen(3030, function() {
    console.log('Server running...');
});

That's it, you've built yourself a basic express application and hopefully have a basic understanding of the routing in express and the view engine jade.
We didn't discuss the inclusion of javascript and css, you can read about it in the article add a public directory to express.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.