简体   繁体   English

带有Nodejs和Express4的基本REST API

[英]Basic REST API with Nodejs and Express4

I have been trying to create a quick REST API using Nodejs and Express4 as I need an API quickly to make a prototype. 我一直在尝试使用Node.js和Express4创建一个快速的REST API,因为我需要一个快速创建原型的API。 While I need all basic CRUD operations, I tried to run the example from the following URL: http://runnable.com/U7bnCsACcG8MGzEc/restful-api-with-node-js-express-4 The example at this address only supports GET, but I believe I could quickly adapt it to support POST, PUT and DELETE. 虽然我需要所有基本的CRUD操作,但是我尝试从以下URL运行示例: http : //runnable.com/U7bnCsACcG8MGzEc/restful-api-with-node-js-express-4此地址处的示例仅支持GET ,但我相信我可以迅速调整它以支持POST,PUT和DELETE。 I do not care of having the data on memory, it is even preferable. 我不在乎将数据存储在内存中,甚至更可取。

You can click to see the code there. 您可以单击此处查看代码。

However, once I have started my server and go on http://localhost:8080/players 但是,一旦启动服务器并继续http://localhost:8080/players

I get "Cannot GET /players" in my browser. 我在浏览器中收到“无法获取/ players”。

Any idea? 任何想法? If somebody has another short/quick example of a REST API with Nodejs with in-memory persistence, I'll take it as well :) 如果有人使用带有内存持久性的Node.js REST API的另一个简短示例,我也将采用它:)

If you want to create quick API with CRUD operation then try npm deployd , but there is not express. 如果要使用CRUD操作创建快速API,请尝试npm deployed ,但没有表达。 If u love to code then use express-generator 如果您喜欢编码,请使用express-generator

Please refer following code to create REST API using ExpressJS. 请参考以下代码以使用ExpressJS创建REST API。

Server.js Server.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 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;

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

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

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