GithubHelp home page GithubHelp logo

hiteshsahu / node-js-es6 Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 5.0 65 KB

Simple setup for using ES6 in Node JS server

License: Apache License 2.0

JavaScript 100.00%
nodejs-server es6-javascript babel-es6 nodemon nodejs

node-js-es6's Introduction

Node JS With ES6

Getting sarted:

Clone or Fork the project

npm install

npm run dev

How to Setup Node JS Server with ES6, Babel, & Nodemon

    import NodeServer from "./server/NodeServer"
    import ExpressServer from "./server/ExpressServer"
    import dotenv from "dotenv"
    dotenv.config()

    // Start Node Server
    const WebServer = new NodeServer();

    //Start Express JS Server
    const expressServer = new ExpressServer();

Build Steps

  1. Install Node js and validate if Node JS is installed properly

node -v

  1. Create project directory and init Node package

mkdir nodeapp && cd nodeapp && npm init -y

  1. Create a src folder in your project's root directory to keep server code.

mkdir src

  1. Create Index.js in src folder as starting point for Server

Set up Babel for precompiling ES6 into JavaScript

Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments.

  1. Install Babel

npm install --save-dev @babel/core @babel/cli

  1. Install Babel Preset & Class Properties Plugin

npm install @babel/preset-env --save-dev
npm install --save-dev @babel/plugin-proposal-class-properties

@babel/preset-env Allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, browser polyfills) are needed by your target environment(s). This both makes your life easier and JavaScript bundles smaller!

@babel/plugin-proposal-class-properties transform ES6 class synthecic sugar into JavaScript __Prototype__

  1. Create .babelrc file in your project's root directory & add the following code in .babelrc

     {
     "presets": ["@babel/preset-env"],
     "plugins": ["@babel/plugin-proposal-class-properties"] 
     }
    

Watch File change with Node Monitor

  1. Install Nodemon

npm i -d nodemon
npm install rimraf --save-dev

Nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.

  1. Update package.json by adding following line for precompiling ES6 and allowing nodemon to restart server when file changes
 "scripts": {
+ "build": "babel src -d dist"
+ "start": "npm run build && node dist",
+ "restart": "rimraf dist && npm run start",
+ "dev": "nodemon --exec npm run restart"
}
  1. Create nodemon.json file in your project's root directory

  2. Add src folder in watchlist by adding following code in nodemon.json

     {"watch": ["src"]}
    

Finally Run the project

npm run dev

node-js-es6's People

Contributors

hiteshsahu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.