Skip to content

Instantly share code, notes, and snippets.

@sriramrudraraju
Last active November 2, 2017 16:51
Show Gist options
  • Save sriramrudraraju/3999a1ec9726593b543ec4bb6d61b49b to your computer and use it in GitHub Desktop.
Save sriramrudraraju/3999a1ec9726593b543ec4bb6d61b49b to your computer and use it in GitHub Desktop.
Creating Express Js seed project

This project was generated using Express.js

Table of Contents

Folder Structure

Once cloned and installed dependencies, project structure looks like

node-seed/
    bin/
        www                      -> start up file and evnvironment setups
    configs/
        bunyan.js                -> bunyan logger config
        database.js              -> database configs
    connectors/
         sql.js                  -> connection and querying sql database
    logs/
         error/
             error.log           -> error and higher priority error logs
         info/
             info.log            -> info and higher level logs
    noe_modules/
    public/
        api-docs/                -> swagger ui documentation static page
    queries/
        process.js               -> query generators for process route
        queries.js               -> query generators for queries route
    routes/
        generate_swagger_json.js -> route for generating swagger.json
        index.js                 -> index route
        queries.js               -> queries route
        users.js                 -> queries route
    views/                       -> view templates (we dont use mostly)
    .gitignore
    app.js                       -> main file
    README.md                                        

Available Scripts

Once project is cloned / downloaded, execute npm install to install the dependencies.

Note : please be aware of few dependencies that needs to installed globally for more features.

npm start

Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.

ctrl + c to stop the server.

npm run start-nodemon

Integrated with nodemon.

Info : Install it globally too. npm install -g nodemon

Note : Manually refresh the page. not adding live-reloads for developement.. dont want to make api calls on each file save.

Best Practices

following are the few best practices for backend server using Node.js

  • compression
  • async functions
  • logging
  • handle exceptions
  • ...more

Process Manager

In development, you started your app simply from the command line with node server.js or something similar. But doing this in production is a recipe for disaster. If the app crashes, it will be offline until you restart it. To ensure your app restarts if it crashes, use a process manager.

use StronLoop Process Manager.

Info : need to install globally. npm install -g strongloop

  • go to app directory and slc start to start the process manager in http://localhost:3001
  • slc ctl -h for more options
  • slc ctl shutdown to shutdown process manager.

Logging

In general, there are two reasons for logging from your app: For debugging and for logging app activity (essentially, everything else).

Integrated with logger module bunyan.

Info: need to install globally for more features. npm install -g bunyan

  • By default logs to console with its properties
  • To display logs prettier node app.js | bunyan add pipe statement to the starting server command.
  • Made the bunyanLog accessable from global object.

Info : Files not created by bunyan. need to manually create .log files.

Warn : Bunyan warns about creating new dynamic logs using rotating feature in node cluster mode.

Swagger UI

Swagger is a specification for describing, producing, consuming, testing, and visualizing a RESTful API. It provides a number of tools for automatically generating documentation based on a given endpoint.

Instructions on setting up for node can be found here

  • Change the port number in routes/generate_swagger_json.js to the port the code is running on.
  • Then fire up http://localhost:3000/swagger.json /swagger.json route was defined in generate_swagger_json. little confusing with name. just to avaoid swagger.json.js which will be more confusing.

After the setup and running

  • Go to public/api-docs/index.html change the swagger.json url at around line 86 to http://localhost:3000/swagger.json or any new url you where you generate the swagger.json file.
  • Go to http://localhost:3000/api-docs/ to see the swagger documentation.

Unit Tests

npm run test to run the test scripts.

Havent tested whether all the dev dependencies work without installing globally. Below are the dependencies that need to be installed globally if needed.

Install mocha globally npm install mocha -g
Install nyc globally npm install nyc -g
Install supervisor globally npm install -g supervisor

  • Add the test scripts in test folder with .spec.js, otherwise mocha doesnt care.
  • http tests can be done using supertest.
  • npm run test-coverage to get coverage report. Coverage reports generated using nyc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment