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.