Node JS and Express Routing Tutorial

Node JS and Express Routing Tutorial

Lets set up our Express app with some different routes now. Lets make a basic company or personal web site structure. With an about,contact,links and portfolio pages.

First lets make the files with Node file system module.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var fs = require('fs');

//   MAKE NEW FILE NAMED INDEX.HTML
fs.open('index.html', 'w', function (err, file) {
  if (err) throw err;
  console.log('SAVED NEW INDEX.HTML FILE');
});
// MAKE ABOUT PAGE
fs.open('about.html', 'w', function (err, file) {
  if (err) throw err;
  console.log('SAVED NEW ABOUT.HTML FILE');
});

// MAKE PORTFOLIO PAGE
fs.open('portfolio.html', 'w', function (err, file) {
  if (err) throw err;
  console.log('SAVED NEW PORTFOLIO.HTML FILE');
});

// MAKE CONTACT PAGE
fs.open('contact.html', 'w', function (err, file) {
  if (err) throw err;
  console.log('SAVED NEW CONTACT.HTML FILE');
});

// MAKE LINKS PAGE
fs.open('links.html', 'w', function (err, file) {
  if (err) throw err;
  console.log('SAVED NEW LINKS.HTML FILE');
});

Then lets append some basic HTML into the files.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// APPEND FILE INDEX.HTML
fs.appendFile('index.html', 'INDEX new text', function (err) {
  if (err) throw err;
  console.log('APPENDING INDEX.HTML FILE');
});

// APPEND FILE ABOUT.HTML
fs.appendFile('about.html', 'ABOUT new text', function (err) {
  if (err) throw err;
  console.log('APPENDING ABOUT.HTML FILE');
});

// APPEND FILE PORTFOLIO.HTML
fs.appendFile('portfolio.html', 'PORTFOLIO new text', function (err) {
  if (err) throw err;
  console.log('APPENDING PORTFOLIO.HTML FILE');
});

// APPEND FILE CONTACT.HTML
fs.appendFile('contact.html', 'CONTACT new text', function (err) {
  if (err) throw err;
  console.log('APPENDING CONTACT.HTML FILE');
});

// APPEND FILE LINKS.HTML
fs.appendFile('links.html', 'LINKS new text', function (err) {
  if (err) throw err;
  console.log('APPENDING LINKS.HTML FILE');
});

Now lets define the routes we want.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var express = require("express");
var app = express();
var path = require("path");

// SERVE ROOT
app.get('/',function(req,res){
  res.sendFile(path.join(__dirname+'/index.html'));
});
//SERVE ABOUT
app.get('/about',function(req,res){
  res.sendFile(path.join(__dirname+'/about.html'));
});
//SERVE PORTFOLIO
app.get('/portfolio',function(req,res){
  res.sendFile(path.join(__dirname+'/portfolio.html'));
});
//SERVE CONTACT
app.get('/contact',function(req,res){
  res.sendFile(path.join(__dirname+'/contact.html'));
});
//SERVE LINKS
app.get('/links',function(req,res){
  res.sendFile(path.join(__dirname+'/links.html'));
});

app.listen(3000);
console.log("Running at Port 3000");

Now we have a web site up with multiple routes.

Related posts

One Thought to “Node JS and Express Routing Tutorial”

  1. Wonderful views on that!

Comments are closed.