/rest_api

Simple Node JS server practice

Primary LanguageJavaScript

Node JS Server

Making a simple server to reference for Node projects.

I have much more experience in Ruby on Rails so wanted to get a Node server demo on the board since that is also very popular in web development.

Followed along here

Run

npm start or yarn start or node index.js

About

Express JS

Express JS simplifies the routing by reducing the need to write a lot of if statements. They have a great reference here

Nodemon

Run the server with Nodemon so you don't have to resetart the server manually

Environment Variables

Using environment variables will help hide keys or offer flexibility in setting values for different environments (development, staging, production).

Here is a great blog from the fine folks at Twilio about different ways to set this up: Working With Environment Variables in Node.js.

Handling POST Request Body

body-parser required now with latest version of express. User-controlled input is untrusted and can fail in multiple ways so middleware is necessary to parse POST requests.

Joi offers a nicely readable syntax for defining and validating body params

Takeaway

As a Ruby on Rails developer I haven't used Node in a production environment yet, but I enjoy writing Javascript and can see myself picking things up quickly from here.