cd to new directory and:
npm init
setup express:
npm install express --save
touch server.js
inside server.js:
const express = require('express');
const app = express();
app.listen(3000, function() {
console.log("listening on 3000")
})
use nodemon to auto restart server:
npm install nodemon --save-dev
create shortcut to start server in package.json:
"scripts": {
"start": "nodemon server.js"
}
npm start
now navigate to http://localhost:3000/ and see:
Cannot GET /
setup ejs template engine:
npm install ejs --save
mkdir views
add to server.js (above app.listen
):
app.set('view engine', 'ejs')
test with mocha and chai:
npm install mocha -g
touch package.json
echo {} > package.json
npm install chai --save-dev
test example:
var chai = require('chai')
var expect = chai.expect;
var Greeting = require('../greeting.js');
describe('Hello', function() {
it('should return 5', function() {
var greeting = new Greeting();
expect(greeting.hello()).to.equal('hello');
});
});