// app.js var express = require('express'); var api = require('./api/getData.js'); var app = express();
app.get('/getData', function (req, res) { api.getData(req, res); });
var server = app.listen(8081, function () { var host = this.address().address; var port = this.address().port; console.log('应用实例,访问地址为 http://%s:%s', host, port); });
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// api/getData.js functiongetData(req, res) { var callback = req.query.callback, num = req.query.num;
var data = []; for (var i = 0; i < num; i++) { data.push({ id: 'id' + i, name: 'Tracy' }); }