如何在Heroku上启用ES2017功能来运行Node.js应用程序? [英] How to run Node.js app with ES2017 features enabled on Heroku?

查看:166
本文介绍了如何在Heroku上启用ES2017功能来运行Node.js应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Node新手,创建了一个应用程序,其中包含一些async / await语法,如下所示:

  const express = require('express'); 
const app = express();

const someLibrary = require('someLibrary');

函数asyncWrap(fn){
return(req,res,next)=> {
fn(req,res,next).catch(next);
};
};

app.post('/ getBlock',asyncWrap(async(req,res,next)=> {
let block = await someLibrary.getBlock(req.body.id);
[多一些代码]
}));

app.listen(process.env.PORT || 8000);

在我的机器上工作正常,但是当我部署到Heroku时,出现错误,因为语法不是支持:

  2017-03-23T10:11:13.953797 + 00:00 app [web.1]:app.post '/ getBlock',asyncWrap(async(req,res,next)=> {
2017-03-23T10:11:13.953799 + 00:00 app [web.1]:SyntaxError:Unexpected token(

让Heroku支持这种语法最简单的方法是什么?

解决方案

指定要在package.json中使用的节点版本: https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version



因此,对于异步/等待支持,您需要指定> = 7.6.0

  {
engines:{
node:> = 7.6.0
}
}
pre>

I'm new to Node and created an app that has some async/await syntax in it like so:

const express = require('express');
const app = express();

const someLibrary = require('someLibrary');

function asyncWrap(fn) {
  return (req, res, next) => {
    fn(req, res, next).catch(next);
  };
};

app.post('/getBlock', asyncWrap(async (req,res,next) => {
  let block = await someLibrary.getBlock(req.body.id);
  [some more code]
}));

app.listen(process.env.PORT || 8000);

It works fine on my machine but when I deploy to Heroku I get an error because the syntax is not supported:

2017-03-23T10:11:13.953797+00:00 app[web.1]: app.post('/getBlock', asyncWrap(async (req,res,next) => {
2017-03-23T10:11:13.953799+00:00 app[web.1]: SyntaxError: Unexpected token (

What is the easiest way to get Heroku to support this syntax?

解决方案

Specify the node version you want to use in your package.json: https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version

So for async/await support you'll want to specify >= 7.6.0

{
  "engines": {
    "node": ">= 7.6.0"
  }
}

这篇关于如何在Heroku上启用ES2017功能来运行Node.js应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆