How to run simple app with koa2?

半腔热情 提交于 2019-12-02 01:39:11

Solution

I was able to find workaround and will describe solution that includes installing Babel module

Step 1 - Install Babel and required presets

$ npm install babel-core --save
$ npm install babel-preset-es2015-node5 --save
$ npm install babel-preset-stage-3 --save

Step 2 - Create index.js file with babel-core/register requirement

// set babel in entry file
require('babel-core/register')({
    presets: ['es2015-node5', 'stage-3']
});

require('./app');

Step 3 - Put your sample code inside of app.js

import Koa from 'koa';    
const app = new Koa();

// Setup handler.
app.use(async ctx => {
    ctx.body = "Hello World!";
});

// Start server.
app.listen(3000);

After running node index.js server works like a pie and import, async, await are being processed properly.

References

Upgraded your node version to at least 7.6 after that there is no need to transpile your code using babel which is highly avoidable in production.Node >7.6 version support async/await which is very powerful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!