-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
35 lines (30 loc) · 922 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var util = require('util');
var path = require('path');
var util = require('util');
var koa = require('koa');
var router = require('koa-router');
var webpack = require('webpack');
var webpackDevMiddleware = require('koa-webpack-dev-middleware');
var compiler = webpack(require('./webpack.config'));
var port = 8000;
var app = koa();
app.use(router(app));
app.use(webpackDevMiddleware(compiler, {}));
app.use(require('koa-serve-index')(process.cwd()));
app.use(require('koa-static')(process.cwd()));
// 模拟jsonp
app.get('/interface', function *(next) {
var query = this.query || {};
var callback = query.callback || "";
var tpl = callback ? callback + "(%s)" : "%s";
var data = {
status: 1,
message: "请求错误",
data: {}
};
this.type = 'text/javascript';
this.body = util.format(tpl,JSON.stringify(data,4,4));
yield next;
});
app.listen(port);
console.log('server start at ' + port);