Skip to content

Latest commit

 

History

History
167 lines (131 loc) · 3.38 KB

File metadata and controls

167 lines (131 loc) · 3.38 KB

Node.js Express4 Boilerplate

  1. Setup
  2. Unit Tests

Setup

  1. Init npm

    npm init
    
  2. adapt package.json

    "private": true,  	//prevents accidental publication of private repositories.
    
  3. Install Express & and Bodyparser

    npm install express --save 		// the HTTP-server
    npm install body-parser --save  // simplifies parsing the content in the body in POST-requests)	
    
  4. create file for server (in windows command line)

    copy NUL > server.js
    

    ...and add server-code:

    const express = require('express');
    const path = require('path');
    const app = express();
    const bodyParser = require('body-parser');
    
    // create application/json parser 
    var jsonParser = bodyParser.json();
     
    // create application/x-www-form-urlencoded parser 
    var urlencodedParser = bodyParser.urlencoded({ extended: false });
    
    app.use(express.static( path.join(__dirname, 'public')));
    
    app.get('/', function(req, res) {
    	res.sendfile('index.html');	
    });
    
    app.get('/api/user', function (req, res) {
      res.send('Hello User');
    });
    
    // app.post('/api/user', function (req, res) {
      // res.send('Got a POST request');
    // });
     
    // POST JSON bodies 
    app.post('/api/user', jsonParser, function (req, res) {
      console.log(req.body);
      if (!req.body){
    	return res.sendStatus(400);
      }
      res.send("welcome json: " + JSON.stringify(req.body));  
    })
    
    // POST urlencoded bodies 
    app.post('/api/login', urlencodedParser, function (req, res) {
      if (!req.body){
    	return res.sendStatus(400);
      }
      res.send('welcome urlencoded ' + req.body.username);
    })
    
    //Respond to a PUT request to the /user route:
    app.put('/api/user', function (req, res) {
      res.send('Got a PUT request at /user');
    });
    
    //Respond to a DELETE request to the /user route:
    app.delete('/api/user', function (req, res) {
      res.send('Got a DELETE request at /user');
    });
    
    app.listen(3000, function () {
      console.log('Example app listening on http://127.0.0.1:3000')
    });
    
    
  5. Create "public" folder and create index.html file with markup

    <!doctype html>
    <html class="no-js" lang="">
    	<head>
    		<meta charset="utf-8">
    		<meta http-equiv="x-ua-compatible" content="ie=edge">
    		<title>My WebApp</title>        
    		<meta name="viewport" content="width=device-width, initial-scale=1">
    		<link rel="stylesheet" href="css/main.css">
    	</head>
    	<body>       
    		<p>Hello world! This is HTML5 Boilerplate.</p>		
    		<img src="img.jpg" />
    		<script src="js/main.js"></script>      
    	</body>
    </html>
    
  6. Start server

    node server.js
    
  7. Open browser / Postman and test routes

    http://127.0.0.1:3000
    http://127.0.0.1:3000/api/user
    

Unit Testing

  1. Install Mocha

    npm install mocha --save-dev
    
  2. Add this to package.json

    "test": "mocha --reporter list"
    
  3. Create a /test directory (mochas default directory for unit-tests)

    mkdir test   
    
  4. Create a file for the tests (in "/test")#

    copy NUL > test.js
    
  5. Insert tests

    var assert = require("assert");
    
    describe('project successfully started', function() {
      it('should have a defined myVar', function() {
    	assert.notEqual('undefined', MyProject.myVar);
      }),
    
      it('should be able to do xyz', function() {    
    	var myObj = new BigObject(0);
    	assert.equal(0, myObj.x);
      })
    })
    
  6. Run the tests

    npm test