简体   繁体   English

从NodeJS服务器使用jQuery请求数据

[英]Request data using jQuery from a NodeJS server

I have the following client code (that runs in a browser or atom-shell/node-webkit Web API): 我有以下客户端代码(在浏览器或atom-shell / node-webkit Web API中运行):

$(document).ready(function(){

    $.ajax({
        url: 'http://127.0.0.1:1337/users',
        type: 'GET',
        dataType: 'json',
        success: function(res)
        {
            console.log(res);
        }
    });

});

Pretty simple stuff, it requests a list of users in JSON format from a server. 很简单的东西,它从服务器请求JSON格式的用户列表。

Now on the server end (Node API) I have the following code: 现在在服务器端(节点API)上,我有以下代码:

var http = require('http');
var mysql = require('mysql');

var connection = mysql.createConnection({
  host     : '127.0.0.1',
  user     : 'admin',
  password : 'qwe123'
});

// Create the server
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('This is just a placeholder for the index of the server.');
}).listen(1337, '127.0.0.1');    

// Build a response
http.get('http://127.0.0.1:1337/users', function(res) {
  var json = { 'status' : res.statusCode, 'response' : res };
  JSON.parse(json);
}).on('error', function(e) {
  var json = { 'status' : e.statusCode, 'message' : e.message };
  JSON.parse(json);
});

I've become confused as how to create the server and then effectively create endpoints (eg /users) that I can talk to via my client code. 我对如何创建服务器然后有效地创建可以通过客户端代码与之交谈的端点(例如,/ users)感到困惑。

1.) Am I right in thinking that http.get only gets data from an already established endpoint? 1.)我认为http.get仅从已经建立的端点获取数据是正确的吗? And that createSever actual creates the endpoints? createSever实际上创建了端点吗? Or is there another shorthand method for creating these endpoints after the server has been created? 还是在创建服务器之后还有另一种简便的方法来创建这些端点? So basically that get request isn't required in this example as I am wanting to request it client side. 所以基本上这个例子中不需要get请求,因为我想在客户端请求它。

2.) In any case I need to create this endpoint /users and return the following: 2.)无论如何,我需要创建此端点/users并返回以下内容:

connection.connect();
connection.query('SELECT * FROM users', function(err, results) {
    JSON.parse(results);
});
connection.end();

Can anyone help point me back in the right direction? 谁能帮助我指出正确的方向? Seems like I've missed that part in the docs where you can create the different endpoints on your server. 似乎我已经错过了文档中的该部分,您可以在其中在服务器上创建不同的端点。

I am afraid you are mixing expressjs and node http module. 恐怕您正在混合expressjs和node http模块。 If you are just using node "http", refer this: Nodejs to provide 1 api endpoint e one html page on how to achieve the routing of URL's. 如果您仅使用节点“ http”,请参考以下内容: Nodejs提供1个api端点以及一个有关如何实现URL路由的html页面

All said, i would suggest you to take a look at the library expressjs for this: http://expressjs.com/starter/basic-routing.html . 所有人都说过,我建议您看一下库expressjs: http ://expressjs.com/starter/basic-routing.html。 It largely simplifies the route management and provides much more functionality. 它在很大程度上简化了路由管理并提供了更多功能。

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM