Labels

javascript (9) css (3) babel (1) nodejs (1) webpack (1)

Friday, April 7, 2017

Automatic Semicolon Insertion (ASI)

I just ​watched a video on best practices for JavaScript. Now I have always been in the camp of "Use semicolons!" and this video didn't change it. There are so many errors that can creep in if you don't know the rules of how ASI works. For instance:

console.log("Log")
[1,2,3].forEach(function(value) {
   // stuff
})

This previous line of code will cause an error because brackets are perfectly acceptable after an object as far as JavaScript is concerned.

Another thing brought up was using curly braces on the same line of the function or anything else that is using them because if you try to do something like this:

function() {
   return
   {
       x: x
   }
}

You will get an error because ASI will put a semicolon right after return. The way to fix this would be to put the curly brace on the same line as return. As an aside, this means to put curly braces on the same line for everything to keep things consistent.

Another reason the author suggested for using semicolons was to stay consistent with other C-like languages because JavaScript itself is a C-like language.

I don't want to tell you how to live your life but I would sure like to make things easier on you so USE SEMICOLONS!...Please.

Update:

In case you needed more convincing, this is from the Google JavaScript style guide: Every statement must be terminated with a semicolon. Relying on automatic semicolon insertion is forbidden. Semicolons are required

Node Web API

Recently I have been working with some Web API and I am forced down the path of Java. So far, I must say that I am confident in this assertion: creating a Web API is much simpler in Node.

It's not that I don't like Java but I am more familiar with Javascript and Node seems to be more suited to the task of Web APIs. This isn't to say it is the best and Java certainly has its own advantages. For instance, Java is more powerful. As a side note, I have made Web APIs in C#.NET and I am pretty impressed with their implementation.

All that aside, I wanted to show a quick how to on making a Web API in Node.

let express = require('express');
let app = express();
let bodyParser = require('body-parser');
let cors = require('cors');

app.use(bodyParser.json());
app.use(cors());

app.post('/api/v1/:resource', function(req, res) {
   let resource = req.params.resource,
       body = req.body;
       
   // do something with the JSON in the body
   
   res.sendStatus(200);
});

app.get('/api/v1/:resource', function(req, res) {
   let resource = req.params.resource,
       body = getData(); //ficticious method for getting the resource
   
   res.send(JSON.stringify(body));
});

app.listen(8888);

So there is a very simple REST interface using Node+Express. You need to install Express, bodyParser, and cors for this to work but that isn't too difficult.

npm install --save express cors bodyParser

bodyParser allows for receiving JSON from the POST body and cors is used to allow cross origin requests. These are not necessary for a simple Web API but you may find yourself needing them.

app.post tells express to run the provided function whenever the url is POSTed to. In this instance, :resource will be whatever is put in that location of the URL.

http://domain/api/v1/users maps :resource to users

app.get works similarly but instead of changing the data, it returns it in the body of the response.

Finally, app.listen tells the server to listen for requests on the specified port.

Start it up using npm start and you are ready to go!