In my previous article 10 Tips to Become a Better Node Developer I introduced 10 Node.js best practices you could apply to your code today. This post continues in that vein with a further 10 best practices to help you take your Node skills to the next level. This is what we're going to cover:
- Use npm scripts — Stop writing bash scripts when you can organize them better with npm scripts and Node. E.g.,
npm run build
,start
andtest
. npm scripts are like the single source of truth when Node developers look at a new project. - Use env vars — Utilize
process.env.NODE_ENV
by setting it todevelopment
, orproduction
. Some frameworks will use this variable too, so play by the convention. - Understand the event loop —
setImmediate()
is not immediate whilenextTick()
is not next. UsesetImmediate()
orsetTimeout()
to offload CPU-intensive tasks to the next event loop cycle. - Use functional inheritance — Avoid getting into mindless debates and a brain-draining trap of debugging and understanding prototypal inheritance or classes by just using functional inheritance like some of the most prolific Node contributors do.
- Name things appropriately — Give meaningful names which will serve as a documentation. Also, please no uppercase filenames, use a dash if needed. Uppercase in filenames not just look strange but can cause cross-platform issues.
- Consider using CoffeeScript — ES6/7 is pathetic addition which was born out of 6 years of meetings when we already had a better JavaScript called CoffeeScript. Use it if you want ship code faster and stop wasting time debating
var
/const
/let
, semi-colons,class
and other arguments. - Provide native code — When using transpilers, commit native JS code (result of the builds) so your projects can run without the builds
- Use gzip — Duh!
npm i compression -S
and sane logging — not too much not to little depending on the environment.npm i morgan -S
- Scale up — Start thinking about clustering and having stateless services from day one of your Node development. Use pm2 or strongloop's cluster control
- Cache requests — Get maximum juice out of your Node servers by hiding them behind a static file server such as nginx and/or request level cache like Varnish Cache and CDN caching.
So let's bisect and take a look at each one of them individually. Shall we?
Use npm Scripts
It's almost a standard now to create npm scripts for builds, tests, and most importantly to start the app. This is the first place Node developers look at when they encounter a new Node project. Some people (1, 2, 3, 4) have even ditched Grunt, Gulp and the likes for the more low-level but more dependable npm script. I can totally understand their argument. Considering that npm scripts have pre and post hooks, you can get to a very sophisticated level of automation:
"scripts": {
"preinstall": "node prepare.js",
"postintall": "node clean.js",
"build": "webpack",
"postbuild": "node index.js",
"postversion": "npm publish"
}
Often times when developing for the front-end, you want to run two or more watch processes to re-build your code. For example, one for webpack and another for nodemon. You can do this with &&
since the first command won't release the prompt. However, there's a handy module called concurrently which can spawn multiple processes and run them at the same time.
Also, install dev command line tools such as webpack, nodemon, gulp, Mocha, etc. locally to avoid conflicts. You can point to ./node_modules/.bin/mocha
for example or add this line to your bash/zsh profile (PATH!):
export PATH="./node_modules/.bin:$PATH"
Use Env Vars
Utilize environment variables even for the early stages of a project to ensure there's no leakage of sensitive info, and just to build the code properly from the beginning. Moreover, some libraries and frameworks (I know Express does it for sure) will pull in info like NODE_ENV
to modify their behavior. Set it to production
. Set your MONGO_URI
and API_KEY
values as well. You can create a shell file (e.g. start.sh
) and add it to .gitignore
:
NODE_ENV=production MONGO_URL=mongo://localhost:27017/accounts API_KEY=lolz nodemon index.js
Nodemon also has a config file where you can put your env vars (example):
{
"env": {
"NODE_ENV": "production",
"MONGO_URL": "mongo://localhost:27017/accounts"
}
}
Continue reading %10 Node.js Best Practices: Enlightenment from the Node Gurus%
by Azat Mardan via SitePoint
No comments:
Post a Comment