Use the koa2 framework instance in nodejs6 and nodejs6koa2

Source: Internet
Author: User

Use the koa2 framework instance in nodejs6 and nodejs6koa2

Koa2 uses ES7 syntax, such as async and await, so it needs to run after node7.6. But before node7.6, you can also run it using koa2 of babel.

First, install babel and several babel modules in the project:

npm install babel babel-register babel-preset-env --save

Then introduce the 'babel-register 'module to the entry file.

require('babel-register');

Then introduce the Business Code:

require('./server.js');

In the configuration. babelrc file:

{ "presets": [  ["env", {   "targets": {    "node": true   }  }] ]}

Example:

App. js:

require('babel-register');require('./servers/devserver');

Devserver. js:

var koa = require('koa');var app = new koa();const request = require('request');let port = process.env.PORT || 8080;console.log("set port:" + process.env.PORT + "; ip:" + process.env.IP);app.use(async (ctx) => { console.log(ctx.url); if (ctx.url.indexOf('/aaa') > -1) {    ctx.response.set('content-type', 'text/javascript');  ctx.body = request.get('http://127.0.0.1/aa.bundle.js', function(err, response, body) {   console.log(body);  }); }});app.listen(port);

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.