node, npm, mocha, grunt, and express
Table of Contents

node

Install node. Here is how to build it from scratch:

$ curl https://nodejs.org/dist/v8.12.0/node-v8.12.0.tar.gz > node-v8.12.0.tar.gz

$ tar xf node-v8.12.0.tar.gz

$ cd node-v8.12.0

$ ./configure

$ make

$ make install

Create a project directory called hello somwhere and make it your working directory. Create a file called server.js with the following contents:

$ cat server.js
let http = require('http');

http.createServer(function(request, response){
  response.writeHead(200, {'Content-type': 'text/plain'});
  response.write('Hello, World!');
  response.end( );

}).listen(8888);

You can run this with

$ node server.js

Navigate to http://localhost:8888/ to see what it does.

npm

npm is a JavaScript package manager. It is installed with recent versions of node.

A project managed by npm will have a package.json file at the root. Typically one runs npm install on such a project after checking it out.

Create an empty package.json and see what happens when you run npm install:

$ echo '{}' > package.json

$ npm install

npm creates a package-lock.json file and tells you to commit it to your project. It also warns about some missing fields in the package.json file.

If a package is installed with npm install and there is a package.json at the root, npm updates the project dependencies:

$ npm install moment

$ cat package.json
{
  "dependencies": {
    "moment": "^2.22.2"
  }
}

A node_modules subdirectory is created. We shouldn't check this directory or any of its contents into our repository. If we are using git, we add node_modules to our .gitignore file.

We can modify server.js to use our dependency:

$ cat server.js
let http = require('http');
let moment = require('moment');

http.createServer(function(request, response){
  let t = moment(new Date());
  let fmt = 'YYYY-MM-DD HH:mm:ss';

  response.writeHead(200, {'Content-type': 'text/plain'});
  response.write('The time is: ' + t.format(fmt));
  response.end( );

}).listen(8888);

Run node server.js and see what the new server does.

If we run the following command:

$ npm start

it will also run node server.js. We can make npm start do something else. Here we make it run node index.js instead:

$ mv server.js index.js

$ vim package.json

$ cat package.json
{
  "dependencies": {
    "moment": "^2.22.2"
  },
  "scripts": {
      "start": "node index.js"
  }
}

$ npm start

mocha

There is an npm package called mocha which can be used for implementing unit tests:

$ npm install -g mocha

$ cat test.js         
let assert = require('assert');

describe('hello', function() {
    it('1 is equal to itself', function() {
    assert.equal(1, 1);
    });
});

$ mocha test.js

If we want to run our tests using npm we can add a "tests" script to our package.json:

$ cat package.json
{
  "dependencies": {
    "grunt": "^1.0.3",
    "moment": "^2.22.2"
  },
  "scripts": {
    "test": "mocha test.js",
    "start": "node index.js"
  }
}

$ npm test

grunt

When doing JavaScript frontend development, there are some tasks are a number of tasks you might want to do which will introduce a build step into your development process:

  • bundling javascript
  • css precompilers
  • browser shims (aka polyfills)
  • minifiers
  • compiling to javascript from other languages

You might want to compile to javascript from another language on the backend as well.

grunt is a build tool which allows build steps to be specified in JavaScript. Here is an example:

$ npm install -g grunt-cli

$ npm install grunt

$ cat Gruntfile.js
module.exports = function(grunt) {
  grunt.registerTask('hello', function() {
    grunt.log.write('Hello, World!');
  });
};

$ grunt hello

plugins

grunt-mocha

grunt-eslint

express

$ npm install express --save

$ cat index.js
let express = require('express')
let app = express()
let moment = require('moment');

app.get('/', function (req, res) {
  res.send('Hello, World!');
});

app.get('/time', function (req, res) {
  let t = moment(new Date());
  let fmt = 'YYYY-MM-DD HH:mm:ss';

  res.send('The time is: ' + t.format(fmt));
});

app.listen(8888);

$ npm start

Visit the URLs http://localhost:8888/ and http://localhost:8888/time

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License