【问题标题】:Basic REST API with Nodejs and Express4使用 Nodejs 和 Express4 的基本 REST API
【发布时间】:2014-09-25 18:58:42
【问题描述】:

我一直在尝试使用 Nodejs 和 Express4 创建一个快速的 REST API,因为我需要一个 API 来快速制作原型。虽然我需要所有基本的 CRUD 操作,但我尝试从以下 URL 运行示例:http://runnable.com/U7bnCsACcG8MGzEc/restful-api-with-node-js-express-4 此地址的示例仅支持 GET,但我相信我可以快速调整它以支持 POST、PUT 和 DELETE。我不在乎将数据保存在内存中,它甚至更可取。

您可以点击查看那里的代码。

但是,一旦我启动了我的服务器并继续 http://localhost:8080/players

我在浏览器中收到“Cannot GET /players”。

有什么想法吗?如果有人有另一个带有内存持久性的带有 Nodejs 的 REST API 的简短/快速示例,我也会接受它:)

【问题讨论】:

  • 你做了“npm install”吗?
  • 两点:首先,在示例中,port 使用的是80,而不是8080。其次,你应该试试http://localhost:8080/api/players,或者http://localhost:80/api/players(我不知道你用的是哪个号码)。
  • @Roy 是的,我确实做了一个“npm install”。
  • @Rodrigo - 是的,我在 server.js 中将端口从 80 更改为 8080。对不起,我忘了提到这一点。关于 URL,您是对的:localhost:8080/api/players 有效。谢谢!

标签: javascript node.js api rest express


【解决方案1】:

如果你想用 CRUD 操作创建快速 API 可以试试 npm deployd ,但是没有 express。如果您喜欢编码,请使用 express-generator

【讨论】:

    【解决方案2】:

    请参考以下代码使用 ExpressJS 创建 REST API。

    服务器.js

    var express = require("express");
    var mysql   = require("mysql");
    var bodyParser  = require("body-parser");
    var rest = require("./REST.js");
    var app  = express();
    
    function REST(){
        var self = this;
        self.connect_to_mysql();
    }
    
    REST.prototype.connect_to_mysql = function() {
        var self = this;
        var pool      =    mysql.createPool({
            connectionLimit : 100,
            host     : 'localhost',
            user     : 'root',
            password : '',
            database : 'restful_api_demo',
            debug    :  false
        });
        pool.getConnection(function(err,connection){
            if(err) {
              self.stop(err);
            } else {
              self.configureExpress(connection);
            }
        });
    }
    
    REST.prototype.configureExpress = function(connection) {
          var self = this;
          app.use(bodyParser.urlencoded({ extended: true }));
          app.use(bodyParser.json());
          var router = express.Router();
          app.use('/api', router);
          var rest_router = new rest(router,connection);
          self.start_server();
    }
    
    REST.prototype.start_server = function() {
          app.listen(3000,function(){
              console.log("All right ! I am alive at Port 3000.");
          });
    }
    
    REST.prototype.stop = function(err) {
        console.log("ISSUE WITH MYSQL \n" + err);
        process.exit(1);
    }
    
    new REST(); // Instantiate class.
    

    REST.js

    function REST_ROUTER(router,connection) {
        var self = this;
        self.handle_routes(router,connection);
    }
    
    REST_ROUTER.prototype.handle_routes = function(router,connection) {
        router.get("/",function(req,res){
            res.json({"Message" : "Hello World !"});
        })
    }
    
    module.exports = REST_ROUTER;
    

    链接:http://codeforgeek.com/2015/03/restful-api-node-and-express-4/

    【讨论】:

      猜你喜欢
      • 2018-10-12
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多