Sequelize和Sequelize-Restful没有回应

时间:2015-09-11 05:54:06

标签: postgresql rest sequelize.js

我是一个快速/ SQL noob尝试使用postgres和sequelize ORM构建报告应用程序。为了使服务更加灵活,我尝试使用sequalize-restful-extended (Author's Github)自动生成API路由,但似乎无法获得任何回报。

当我将 http://localhost:8080/api 扔进Postman时,API会一直加载,直到我关闭响应我从我的摩根日志中得到一个空白的回复(:: 1 - - [11 / Sep / 2015 :05:52:14 +0000]“GET / api HTTP / 1.1” - - “ - ”“)

任何帮助或指导都会得到很好的赞赏。

var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var Sequelize = require('sequelize');
var http = require('http');
var restful   = require('sequalize-restful-extended');
var env       = process.env.NODE_ENV || 'development';
var config    = require(__dirname + '/bin/config.json')[env];
var database = new Sequelize(config.database, config.username, config.password, {
                host: config.host,
                port: config.port,
                logging: console.log,
                dialect: config.dialect
              });

var app = express();


// ROUTE REQUIRES
var port = process.env.PORT || 8080;

// MIDDLEWARES
app.use(logger('common'));
app.use(bodyParser());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));





  var categories = database.define('categories', { 
    CategoryID: {
      type: Sequelize.INTEGER,
      allowNull: false,
      identifier: {type: Sequelize.INTEGER, primaryKey: true}
    },
    CategoryName: {
      type: Sequelize.STRING,
      allowNull: false
    },
    Description: {
      type: Sequelize.TEXT,
      allowNull: true
    },
    Picture: {
      type: Sequelize.BLOB,
      allowNull: true
    }
  });

  var products = database.define('products', { 
    ProductID: {
      type: Sequelize.INTEGER,
      allowNull: false,
      identifier: {type: Sequelize.INTEGER, primaryKey: true}
    },
    ProductName: {
      type: Sequelize.STRING,
      allowNull: false
    },
    SupplierID: {
      type: Sequelize.INTEGER,
      allowNull: true
    },
    CategoryID: {
      type: Sequelize.INTEGER,
      allowNull: true
    },
    QuantityPerUnit: {
      type: Sequelize.STRING,
      allowNull: true
    },
    UnitPrice: {
      type: Sequelize.REAL,
      allowNull: true
    },
    UnitsInStock: {
      type: Sequelize.INTEGER,
      allowNull: true
    },
    UnitsOnOrder: {
      type: Sequelize.INTEGER,
      allowNull: true
    },
    ReorderLevel: {
      type: Sequelize.INTEGER,
      allowNull: true
    },
    Discontinued: {
      type: Sequelize.INTEGER,
      allowNull: false
    }
  });

//FIND A PRODUCT'S CATEGORY
categories.belongsTo(products)





// RESTIFY ORM
app.use(restful(database));


// SERVER CONNECT
app.listen(port);
console.log("You're listening to http://localhost:" + port + " home of the internet's smoothest jazz and easy listening" );






// CATCH ALL FOR HTML 5 MODE (allows UI-Router driven states)
app.get('*', function (req, res) {
  res.redirect('/#' + req.originalUrl);
});

2 个答案:

答案 0 :(得分:0)

从我看到你使用过

app.use(restful(database));

vs

app.configure(function() {
  app.use(restful(sequelize, { /* options */ }))
})
manual

建议

这可能是你的问题吗?

答案 1 :(得分:0)

当我尝试运行您的代码时,注意到以下错误:

$ node app1.js 
body-parser deprecated bodyParser: use individual json/urlencoded middlewares app1.js:31:9
body-parser deprecated undefined extended: provide extended option node_modules/body-parser/index.js:105:29
You're listening to http://localhost:8080 home of the internet's smoothest jazz and easy listening

::ffff:127.0.0.1 - - [12/Sep/2015:22:34:12 +0000] "GET /api HTTP/1.1" 500 2546
TypeError: Cannot read property 'daos' of undefined
at handleIndex (/home/haris/Documents/stack-overflow/Sequelize-Restful/node_modules/sequelize-restful-extended/lib/router.js:248:48)
at Router.handleRequest (/home/haris/Documents/stack-overflow/Sequelize-Restful/node_modules/sequelize-restful-extended/lib/router.js:47:21)
at /home/haris/Documents/stack-overflow/Sequelize-Restful/node_modules/sequelize-restful-extended/lib/index.js:10:16
at multipart (/home/haris/Documents/stack-overflow/Sequelize-Restful/node_modules/sequelize-restful-extended/node_modules/connect/lib/middleware/multipart.js:64:37)
...

并使用'curl -i enter image description here'进行测试。

更深入我在/node_modules/sequelize-restful-extended/package.json中确定了sequelize-restful-extended模块基于"sequelize": "1.7.0-rc2", devDependencie。

然而,在主应用程序sequelize模块安装了它的最新版本1.7.11:

$ npm install sequelize --save
npm WARN package.json @ No description
npm WARN package.json @ No repository field.
npm WARN package.json @ No README data
npm WARN package.json @ No license field.
npm WARN deprecated lingo@0.0.5: This project is abandoned
sequelize@1.7.11 node_modules/sequelize
├── commander@2.1.0
├── dottie@0.1.0
├── toposort-class@0.3.1
├── generic-pool@2.0.4
├── node-uuid@1.4.3
├── validator@3.2.1
├── lingo@0.0.5
├── underscore.string@2.3.3
├── bluebird@1.0.8
├── lodash@2.4.2
├── moment@2.5.1
├── sql@0.35.0 (sliced@0.0.5, lodash@1.3.1)
└── circular-json@0.1.6 (wru@0.2.7)

$ cat package.json 
{
  "dependencies": {
    "body-parser": "~1.13.3",
    "epilogue": "^0.6.1",
    "express": "~4.13.3",
    "morgan": "~1.6.1",
    "path": "~0.11.14",
    "pg": "^4.4.1",
    "pg-hstore": "^2.3.2",
    "sequelize": "^1.7.11",
    "sequelize-restful-extended": "^0.1.4"
  }
}

因此,解决方案是调整版本sequelize

  • 从主应用

    中删除最新版本的sequelize
    $ npm uninstall sequelize
    
  • 然后安装版本1.7.0-rc2,其中sequelize-restful-extended模块依赖

    $ npm install sequelize@1.7.0-rc2 --save
    

最后,package.json看起来如下:

$ cat package.json 
{
  "dependencies": {
    "body-parser": "~1.13.3",
    "epilogue": "^0.6.1",
    "express": "~4.13.3",
    "morgan": "~1.6.1",
    "path": "~0.11.14",
    "pg": "^4.4.1",
    "pg-hstore": "^2.3.2",
    "sequelize": "^1.7.0-rc2",
    "sequelize-restful-extended": "^0.1.4"
  }
}

然后app按预期运行

$ node app1.js 
body-parser deprecated bodyParser: use individual json/urlencoded middlewares app1.js:31:9
body-parser deprecated undefined extended: provide extended option node_modules/body-parser/index.js:105:29
You're listening to http://localhost:8080 home of the internet's smoothest jazz and easy listening
::ffff:127.0.0.1 - - [12/Sep/2015:23:02:37 +0000] "GET /api HTTP/1.1" 200 119

并按预期做出回应

$ curl -i http://localhost:8080/api
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 119
ETag: W/"77-dGBloVSmkq0I3FzWdjCdaQ"
Date: Sat, 12 Sep 2015 23:03:12 GMT
Connection: keep-alive

{"status":"success","data":[{"name":"categories","tableName":"categories"},{"name":"products","tableName":"products"}]}