斗士狗 2016-06-18 08:57 采纳率: 0%
浏览 175

使用Node.js的Ajax请求

After looking around google and stack overflow, I'm yet to find a way to post data to my Node.js server.

There seems to be disparity between the methods I've seen, presumably due to depreciation.

One method I saw was this code:

var express = require('express');
var app = express.createServer();

app.use(express.bodyParser());

app.get('/endpoint', function(req, res){
    var obj = {};
    obj.title = 'title';
    obj.data = 'data';

    console.log('params: ' + JSON.stringify(req.params));
    console.log('body: ' + JSON.stringify(req.body));
    console.log('query: ' + JSON.stringify(req.query));

    res.header('Content-type','application/json');
    res.header('Charset','utf8');
    res.send(req.query.callback + '('+ JSON.stringify(obj) + ');');
});

app.post('/endpoint', function(req, res){
    var obj = {};
    console.log('body: ' + JSON.stringify(req.body));
    res.send(req.body);
});

I get the error:

TypeError: express.createServer is not a function
    at Object.<anonymous> (c:\Users\Tobi\Desktop\ChangeLog\ChangeLog\app.js:2:19)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:146:18)
    at node.js:404:3

I did some reading and saw that Express had stopped coming with "middle-ware" and they would need to be included seperately - I also got an error message saying so.

What is the current / best way to implement Ajax Post requests to my server?

  • 写回答

2条回答 默认 最新

  • weixin_33724046 2016-06-18 09:10
    关注

    Since you're using JSON I would use the json function instead of send this way your app sends the response with the correct content-type and automatically converts the passed string to a JSON object. The body-parser also has a function to work with JSON received from the client.

    Here's some example code:

    var express = require('express');
    var bodyParser = require('body-parser');
    
    var app = express();
    
    app.use(bodyParser.json());
    
    app.post('/endpoint', (req, res) => {
        res.json({
            helloWorld: 'hello world!',
            msg: req.body.message
        });
    });
    
    app.listen(3000, () => {
        console.log('server listening at port 3000');
    });
    

    Old answer

    createServer is deprecated since express 3. You should use the express function instead.

    var express = require('express');
    var app = express();
    

    Edit

    @robertklep mentioned the express bodyparser is also deprecated.

    You can now get this middleware as a separated module via NPM

    npm install body-parser

    var bodyParser = require('body-parser');
    

    Please visit this link for a list of middleware which are now separated from express itself.

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记