DEV Community

Srinivas Kandukuri
Srinivas Kandukuri

Posted on

ES6 Nodejs Project Kickstart

Architecture

When we start new project, we all try to follow the best architecture. Architecture is the starting point to project, A bad project architecture create lot of unnecessary workload to developers and very difficult to introduce new feature to project.

here is the sample snippet of routefile

import express from 'express';
import HelperUtils from '../utils/helperUtils';

const Helperservice = new HelperUtils();
const router = express.Router();

/* This routes Serves application home page */
router.get('/', (req, res) => {
    res.send('Application Running');
});

/* sample end point */
router.get('/getData', async (req, res) => {
    const response = Helperservice.reverse();
    res.json({
        reverseString: response,
        status: 'success',
        statusCode: 200,
    });
});

export default router;
Enter fullscreen mode Exit fullscreen mode

Rule-1 : Standard folder/project architecture

Alt Text

Rule-2 : Best tools used

Rule-3 : Separate router file

Rule-4 : Separate service layer

Rule-5 : Babel integration

Rule-6 : Automated test cases

Rule-7 : Code coverage report

Rule-8 : Es-lint integration

Quick Start

  1. Make sure you have recent, stable version of nodejs in your system. Please check version before run
$  node -v
Enter fullscreen mode Exit fullscreen mode
  1. Clone or download this repository.

  2. Run this following command in your terminal from the project folder

$ npm install
Enter fullscreen mode Exit fullscreen mode

List of Commands/Tasks

Lint

Perform eslint in your project

$ npm run lint
Enter fullscreen mode Exit fullscreen mode

Lint Fix

Most of the errors reported by eslint fixed by using this command

$ npm run lint-fix
Enter fullscreen mode Exit fullscreen mode

Test

This will run all test cases

$ node test 
Enter fullscreen mode Exit fullscreen mode

Generate nyc report -- (optional command)

After testcases passed this will generate nyc report and uploads to codecov

$ node report-coverage 
Enter fullscreen mode Exit fullscreen mode

Build (Transpiled)

This will create '/dist' folder and converts the ES6 code into es5

$ node run build
Enter fullscreen mode Exit fullscreen mode

Start nodejs server

$ node start
Enter fullscreen mode Exit fullscreen mode

NPM Package Details

npm version

Build Status

Build Status

Code Cov

codecov

Top comments (11)

Collapse
 
lagsurfer profile image
Barney

router.get('/getData', async (req, res) => { why is this function async?

Collapse
 
c4r4x35 profile image
Srinivas Kandukuri

In the next line we have written

 const response = Helperservice.reverse();

we can write this method as promise or callback with await

Collapse
 
hilleer profile image
Daniel Hillmann

He probably intented to await on the next line :)

Collapse
 
seanmclem profile image
Seanmclem • Edited

How can I get the import statements to work in nodejs? I keep reading that you set an experimental flag, but how do I do that? Can I get it to be permanent?

Is your babel config taking care of it?

Collapse
 
hilleer profile image
Daniel Hillmann • Edited

I think one of the purposes with Babel here is to compile to an earlier ES version, without import/export syntax.
If you look in the compiled js files, you will probably see require and module.export (this can vary on what Babel is set to build for)

This way you could use earlier node versions and not limit it to just one.

I would say this is more useful in modules than services, as a service could just be locked to one node version, whereas modules are likely to be used in many different versions of node :)

Collapse
 
c4r4x35 profile image
Srinivas Kandukuri

As Daniel said babel here is to compile es6 to es5, like import/export syntax.
it will create a dist folder with compiled code.

Collapse
 
hendrikfoo profile image
Hendrik Richert

Node12+ has native support for import/export:

thecodebarbarian.com/nodejs-12-imp...

Collapse
 
seanmclem profile image
Seanmclem

Yeah I tried upgrading to node 12 this past weekend and was still getting errors suggesting I use an experimental flag. But I can read your link and look into it more

Collapse
 
hilleer profile image
Daniel Hillmann

As also seen in the link that you send, it is only supported natively in modules :-). Thus not in the sample code used in the post ... But beside that you are right.

Collapse
 
lineldcosta profile image
lineldcosta

I think we can use jest for testing. what say?

Collapse
 
c4r4x35 profile image
Srinivas Kandukuri

Yes we can jest as well