DEV Community

Edison Sanchez
Edison Sanchez

Posted on

Node.js - Best Practices

QUICK Creation

  1. Create a folder.
  2. Init Node Project.


    npm init

  3. Create app.js

    var express = require('express');
    

var app = express();

var port = process.env.PORT || 3000;

app.get('/', (req, rest) => {
rest.send('Welcome to my API!');
});

app.listen(port, () => {
console.log(Running on port ${port});
});



4. Install dependencies.
>

Enter fullscreen mode Exit fullscreen mode

npm install express nodemon
npm install --save-dev eslint



5. Add script to package.json
>

Enter fullscreen mode Exit fullscreen mode

...
"scripts": {
...
"start": "nodemon app.js",
"lint": "eslint .",
...
},
...
"nodemonConfig": {
"restartable": "rs",
"ignore": [
"node_modules/**/node_modules"
],
"delay": "2500",
"env": {
"NODE_ENV": "development",
"PORT": 4000
}
}
...



6. Configure ESLINT
>

Enter fullscreen mode Exit fullscreen mode

npm run lint -- --init


Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
aslasn profile image
Ande

For others really looking for best practices, here
github.com/goldbergyoni/nodebestpr...
It has +50k stars so pretty much covers a lot of goods

Collapse
 
tiajackson profile image
TiaJackson

This is a really a good and detailed article on best NodeJs project structure.
Here is a detaied guide on the different best practices which can be followed for any project: tatvasoft.com/blog/node-js-best-pr...

Collapse
 
khuongduybui profile image
Duy K. Bui

What exactly is the "best practice" you are advocating for in this post? The use of eslint?

Collapse
 
aslasn profile image
Ande

I was curious if he mistakenly posted early, draft or something. So i looked into his past posts. There's another one on react which pretty much is a project structure that uses css in js.
dev.to/edisonsanchez/react-best-pr...

Collapse
 
lagsurfer profile image
Barney

nodejs is much more than express 🤷‍♂️🤷‍♂️🤷‍♂️🤷‍♂️