如何使用koa2运行简单的应用程序? [英] How to run simple app with koa2?
问题描述
问题
我正在尝试运行简单的http服务器,使用 koa2 ,但运行它有问题。
它使用<一个href =/ questions / tagged / es6class =post-tagtitle =show questions tagged'es6' =tag> es6 ,预计将来会工作 node.js 版本,我想知道如何运行 node v6.1.0
?
代码
从'koa'导入Koa;
const app = new Koa();
//安装程序。
app.use(async ctx => {
ctx.body =Hello World!;
});
//启动服务器。
app.listen(3000);
输出
< pre class =lang-js prettyprint-override>
$ node --version
v6.1.0
$ node --harmony index.js
C:\Users\gevor\WebstormProjects\untitled1\index.js:1
(function(exports,require,module,__filename,__dirname){import Koa from'koa';
^^^^^
SyntaxError:意外的令牌导入
在exports.runInThisContext(vm.js:53:16)
在Module._compile(module.js:511:25)
在Object.Module._extensions..js(module.js:550:10)
在Module.load(module.js:456:32)
在tryModuleLoad(module.js:415 :$)
在Function.Module._load(module.js:407:3)
在Function.Module.runMain(module.js:575:10)
在启动(节点。 js:160:18)
在node.js:445:3
问题
我想知道如何运行我的应用程序?
类似问题
- a href =https://github.com/koajs/koa/issues/621 =nofollow> https://github.com/koajs/koa/issues/621
- https://github.com/koajs/koa/issues/572
解决方案
我找到解决方法,并将描述包括安装 Babel 模块的解决方案>
步骤1 - 安装 Babel 并且需要预设
$ npm安装babel-core --save
$ npm安装babel-preset-es2015-node5 - 保存
$ npm安装babel-preset-stage-3 --save
步骤2 - 使用 babel-core / register
requiremen创建 index.js
t
//在输入文件中设置babel
require('babel-core / register')({
预设:['es2015-node5','stage-3']
});
require('./ app');
步骤3 - 将您的示例代码放在 app.js
从'koa'导入Koa;
const app = new Koa();
//安装程序。
app.use(async ctx => {
ctx.body =Hello World!;
});
//启动服务器。
app.listen(3000);
运行 node index.js
服务器工程像一个派对, import
, async
, await
正在
参考
Problem
I am trying to run simple http server using koa2, but have problems running it.
It uses es6 that is expected to work in future node.js versions and I was wondering how can I run it with node v6.1.0
?
Code
import Koa from 'koa';
const app = new Koa();
// Setup handler.
app.use(async ctx => {
ctx.body = "Hello World!";
});
// Start server.
app.listen(3000);
Output
$ node --version
v6.1.0
$ node --harmony index.js
C:\Users\gevor\WebstormProjects\untitled1\index.js:1
(function (exports, require, module, __filename, __dirname) { import Koa from 'koa';
^^^^^^
SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:511:25)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:160:18)
at node.js:445:3
Question
I want to know how to run my app?
Similar Issues
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
- koa2 sources
- Documentation should include an example of babel configuration
- Using Babel
- Installation Guide
这篇关于如何使用koa2运行简单的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!