gpt4 book ai didi

rest - 如何使用 Sequelize 查询构建 .json 端点?

转载 作者:行者123 更新时间:2023-12-03 22:42:15 26 4
gpt4 key购买 nike

一个半星期以来,我一直在努力解决这个问题,我想知道如何从 Sequelize ORM 的查询中获取 .json 端点。目前它记录了 404 错误“GET/api/users 404 3ms”。正如您可能听说过 Sequelize 的文档非常有限,并且我在教程之后一直在搜索 github repo,但到目前为止没有任何工作,所以我想我会在这里问。

一小段摘录(https://github.com/NatuMyers/A.M.E.N.SQL-Stack 上的代码):

// VARS -----------------------------
var express = require('express')
, bodyParser = require('body-parser')
, errorHandler = require('errorhandler')
, methodOverride = require('method-override')
, morgan = require('morgan')
, http = require('http')
, path = require('path')
, db = require('./models')

var router = require('express').Router();
var app = express()

// all environments
app.set('port', process.env.PORT || 3000)
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(morgan('dev'))
app.use(bodyParser())
app.use(methodOverride())
app.use(express.static(path.join(__dirname, 'public')))

// SEQUELIZE MODELS
var userVar = require('./models/user');

// dev only
if ('development' === app.get('env')) {
app.use(errorHandler())
}

// Make db, and make it listen
db
.sequelize
.sync()
.complete(function(err) {
if (err) {
throw err
} else {
http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'))
})
}
})

// HTTP GET endpoints
module.exports = function() {

router.get('/', function(req, res, next){
res.json({ message: 'This works at localhost:3000/api but getting a list of users is a pain :(' });
});

// question
router.get('/users', function(req, res, next){
res.json(/* I need to make sequelize send a part of the User db here. */);
});

return router;
};

最佳答案

我通过使用 Epilogue.js(以一种普通的方式)从这里开始。

我使用 Sequelize 添加了 INLINE 模型(我浪费了大量时间尝试导入模型),然后添加任何中间件并根据以下语法创建 restful api。

// 1. ADD SEQUELIZE MODELS ---- ---- ---- ----
var database = new Sequelize('raptroopdb', 'root', 'strongpassword');

var Employee = database.define('Employee', {
name: Sequelize.STRING,
hireDate: Sequelize.DATE
});

// Add Account model with foreign key constraint to Employee
var Account = database.define('Account', {
name: Sequelize.STRING,
managerId: {
type: Sequelize.INTEGER,

references: {
// This is a reference to model Employee
model: Employee,

// This is the column name of the referenced model
key: 'id',

}
}
});

// 2. ROOM FOR MIDDLEWARE to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('In server.js');

// make sure we go to the next routes and don't stop here
next();
});

// Initialize epilogue
epilogue.initialize({
app: app,
sequelize: database
});

app.use(express.static(__dirname + "/public"));
app.get('/', function(req, res) {
res.redirect('/public/index.html');
});



// 3. Create REST resource
var employeeResource = epilogue.resource({
model: Employee,
endpoints: ['/api/employees', '/api/employees/:id']
});

var acctResource = epilogue.resource({
model: Account,
endpoints: ['/api/accounts', '/api/accounts/:id']
});

// Create database and listen
database
.sync({
force: false
})
.then(function() {
app.listen(port, function() {
console.log('listening at %s', port);
});
});

关于rest - 如何使用 Sequelize 查询构建 .json 端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35732405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com