如何使用koa2运行简单的应用程序?

编程入门 行业动态 更新时间:2024-10-11 03:18:06
本文介绍了如何使用koa2运行简单的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题

我正在尝试运行简单的http服务器,使用 koa2 ,但运行它有问题。

它使用 es6 ,预计将来会工作 node.js 版本,我想知道如何运行 node v6.1.0 ?

代码

从'koa'导入Koa; const app = new Koa(); //安装程序。 app.use(async ctx => { ctx.body =Hello World!; }); //启动服务器。 app.listen(3000);

输出

$ 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 =github/koajs/koa/issues/621 =nofollow> github/koajs/koa/issues/621
  • github/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 正在

参考

  • koa2来源
  • 文档应该包括一个babel配置的例子
  • 使用Babel
  • 安装指南

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

  • github/koajs/koa/issues/621
  • github/koajs/koa/issues/572

解决方案

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运行简单的应用程序?

本文发布于:2023-11-25 05:59:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   应用程序   简单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!