This project was generated using Express.js
- Folder Structure
- Available Scripts
- Best Practices
- Process Manager
- Logging
- Swagger UI
- Unit Tests
- TODOS
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
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.
Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.
ctrl + c
to stop the server.
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.
following are the few best practices for backend server using Node.js
- compression
- async functions
- logging
- handle exceptions
- ...more
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 inhttp://localhost:3001
slc ctl -h
for more optionsslc ctl shutdown
to shutdown process manager.
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 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.
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 globallynpm install nyc -g
Install supervisor globallynpm install -g supervisor