代码之家  ›  专栏  ›  技术社区  ›  MegaRoks

如何使用ES6标准启动服务器

  •  0
  • MegaRoks  · 技术社区  · 7 年前

    我有一个服务器,文件index.js的代码如下所示。我想贴在上面 https://dashboard.heroku.com/ . 但它不能启动命令 node inde.js .

    import http from 'http'
    import express from 'express'
    import bodyParser from 'body-parser';
    
    
    const port = process.env.PORT || 8081;
    const app = express();
    const server = http.createServer(app);
    
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({
        extended: false
    }));
    
    app.get('/', (req, res) => res.status(200).send({
        message: 'Welcome',
    }));
    
    server.listen(port, () => {
        console.log(`Server running at ${port}`);
    });
    

    如何使用ES6标准命令运行它 节点索引JS ?我已经安装了 babel7

    1 回复  |  直到 7 年前
        1
  •  1
  •   Seblor Oybek    7 年前

    节点尚不完全支持ES6导入。它仍然是实验性的。要使用它们,您需要将文件扩展名从 .js .mjs 以这个标志开始:

    node --experimental-modules my-app.mjs
    

    更多详细信息: https://nodejs.org/api/esm.html

    推荐文章