博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NodeJS使用ES6
阅读量:2389 次
发布时间:2019-05-10

本文共 938 字,大约阅读时间需要 3 分钟。

虽然Nodejs支持ES6特性越来越完整,但是很可惜模块部分仍然不支持。目前可以通过babel来解决 。

创建项目,并安装相关依赖

{  "name": "nodees6",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1"  },  "author": "",  "license": "ISC",  "devDependencies": {    "babel-core": "^6.25.0",    "babel-preset-es2015": "^6.24.1",    "babel-register": "^6.24.1"  }}
建立babel的配置文件 .babelrc

{  "presets":['es2015']}
创建index.js

require('babel-register')require('./nodees6.js')
采用bebel register实时转译代码。

创建nodees6.js

import http from "http"const server = http.createServer((req, res)=>{    console.log(req.url)    res.write("hello the world")    res.end()})server.listen(9001)
测试

curl http://localhost:9001/hello?user=chf

当然也可以模块部分采用原有的写法,其它可以直接使用ES6的语法。

const http = require('http')const server = http.createServer((req, res)=>{    console.log(req.url)    res.write("hello the world")    res.end()})server.listen(9001)
最终效果也是一样的。

你可能感兴趣的文章
如何检查某个用户是否具有某个权限对象上定义的某种权限
查看>>
为什么使用中间件下载时总是收到警告消息Object is in status Wait
查看>>
使用DOM Breakpoints找到修改属性的Javascript代码
查看>>
Fiori里花瓣的动画效果实现原理
查看>>
利用CRM中间件Middleware从ERP下载Customer Material的常见错误
查看>>
利用Chrome的Heap Snapshot功能分析一个时间段内的内存占用率
查看>>
Document flow API in SAP CRM and C4C
查看>>
One Order行项目里Item Category是怎么计算出来的
查看>>
如何从ERP将Material的Batch信息下载到CRM并存储在settype COMM_PR_BATCH里
查看>>
ABAP和Java里关于DEFAULT(默认)机制的一些语言特性
查看>>
SAPGUI里实现自定义的语法检查
查看>>
如何将iso文件安装到VirtualBox里的ubuntu去
查看>>
让SAP云平台上的Web应用使用destination服务
查看>>
通过一个例子学习Kubernetes里的PersistentVolumeClaim的用法
查看>>
容器,Docker, Kubernetes和Kyma,以及Kyma对SAP的意义
查看>>
推荐一个yaml文件转json文件的在线工具
查看>>
如何查找Authorization object在哪些ABAP代码里使用到
查看>>
使用SAP C4C rule editor动态控制UI上某个按钮是否显示
查看>>
ABAP正则表达式 vs SPLIT INTO
查看>>
使用JDBC操作SAP云平台上的HANA数据库
查看>>