gpt4 book ai didi

mysql - express js - 使用 sequelize 时找不到 GET 请求 404

转载 作者:行者123 更新时间:2023-12-03 22:20:34 24 4
gpt4 key购买 nike

我在服务器开发中使用 Sequelize 和 express.js。
当我尝试发送路由 /files 的 GET 请求时,显示错误:

{
"errors": {
"message": "Not Found",
"error": {
"status": 404
}
}
}
文件夹结构
src
-models
-File.js
-routes
-api
-index.js
-files.js
-index.js
-app.js
-sequelize.js
package.json
我正在尝试从表 files 中获取所有记录
/src/routes/api/files.js
const router = require('express').Router();
const Files = require('../../models/File')

router.get('/files', (req,res) =>
Files.findAll()
.then(data => {
res.sendStatus(200)
})
.catch(err => console.log(err))
);

module.exports = router;

/src/routes/index.js
var router = require('express').Router();

router.use('/api', require('./api'));

module.exports = router;
/src/models/File.js
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;
const db = require('../sequelize')

let files = db.define('files', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true
},
fileName: {
type: DataTypes.STRING
},
});

module.exports = files;
源代码/sequelize.js
const { Sequelize } = require('sequelize');

const sequelize = new Sequelize('myDb', 'admin', '1234', {
dialect: 'mysql',
logging: false
});

sequelize.sync({alter: true})

module.exports = sequelize;
源代码/应用程序.js
var http = require('http'),
path = require('path'),
methods = require('methods'),
express = require('express'),
bodyParser = require('body-parser'),
session = require('express-session'),
cors = require('cors'),
passport = require('passport'),
errorhandler = require('errorhandler'),
mongoose = require('mongoose');

const sequelize = require('./sequelize')

sequelize.authenticate()
.then(() => {console.log("connected")}) //printed "connected"
.catch((err) => {console.log(err)})


var isProduction = process.env.NODE_ENV === 'production';

var app = express();

app.use(cors());

app.use(require('morgan')('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use(require('method-override')());
app.use(express.static(__dirname + '/public'));

app.use(session({ secret: 'conduit', cookie: { maxAge: 60000 }, resave: false, saveUninitialized: false }));

if (!isProduction) {
app.use(errorhandler());
}

if(isProduction){
mongoose.connect(process.env.MONGODB_URI);
} else {
mongoose.connect('mongodb://localhost/conduit');
mongoose.set('debug', true);
}

require('./models/File');
app.use(require('./routes'));

/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

if (!isProduction) {
app.use(function(err, req, res, next) {
console.log(err.stack);

res.status(err.status || 500);

res.json({'errors': {
message: err.message,
error: err
}});
});
}

app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({'errors': {
message: err.message,
error: {}
}});
});

var server = app.listen( process.env.PORT || 3000, function(){
console.log('Listening on port ' + server.address().port);
});

更新 1
src/routes/api/index.js
var router = require('express').Router();

router.use('/', require('./users'));
router.use('/files', require('./files'));

router.use(function(err, req, res, next){
if(err.name === 'ValidationError'){
return res.status(422).json({
errors: Object.keys(err.errors).reduce(function(errors, key){
errors[key] = err.errors[key].message;

return errors;
}, {})
});
}

return next(err);
});

module.exports = router;

最佳答案

编辑
在您的 /api/index.js 文件中,您有:

router.use('/files', require('./files'));
所以 URL 应该是: http://localhost:3000/api/files/files如果要将 URL 更改为: http://localhost:3000/api/files ,则在 files.js 中,应更改为:
router.get('/', (req,res) => // change "/files" to just "/" 
Files.findAll()
.then(data => {
res.sendStatus(200)
})
.catch(err => console.log(err))
);

关于mysql - express js - 使用 sequelize 时找不到 GET 请求 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67515042/

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