Home > Web Front-end > JS Tutorial > Introduction to the method of using es6 in express (detailed)

Introduction to the method of using es6 in express (detailed)

不言
Release: 2018-10-17 14:44:54
forward
3026 people have browsed it

The content of this article is an introduction to the method of using es6 in express (detailed). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The JavaScript standard provided on the express official website is es5, which cannot directly run es6. If you want to use es6 writing in express, you can use the transcoder Babel to transcode.

In development environment

1. Install babel-cli in express project

$ npm install --save-dev babel-cli
Copy after login

2. Install presets

npm install --save-dev babel-preset-es2015 babel-preset-stage-2
Copy after login

3. Add the running script in package.json

{
    ...
    "scripts": {
        "start": "babel-node index.js --presets es2015,stage-2"
     }
    ...
}
Copy after login

4. Now you can use the es6 writing method. Write a paragraph of es6

5. Run

npm start
Copy after login

Just started learning When using express, you will encounter a problem: every time you change a little bit of code, you need to restart the service. We hope to achieve the "hot update" effect. Next, we can use nodemon to monitor file modifications to achieve the hot update effect instead of restarting the service every time.

1. Install nodemon

npm install --save-dev nodemon
Copy after login

2. Modify the script

{
    ...
    "scripts": {
        "start": "nodemon index.js --exec babel-node --presets es2015,stage-2" 
    }
    ...
}
Copy after login

3. Run

   npm start
Copy after login

Now change the js code without restarting the service to achieve the effect

The above is the detailed content of Introduction to the method of using es6 in express (detailed). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template