我无法使我的api与我的Express服务器一起使用,总是出现错误:
错误:未指定默认引擎,也未提供扩展名。
我很确定它来自快递路由器,但我找不到错误的来源...
我是否正确使用快递路由器?因为我以前从未使用过。
server.js:
const createError = require('http-errors');
const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const api = require('./routes/api');
const app = express();
const MongoClient = require('mongodb').MongoClient;
const MONGODB_URI = process.env.MONGODB_URI;
const PORT = process.env.PORT || 5000;
app.use('/', api);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
// Database object
var db;
// Initialize connection once with db
MongoClient.connect(MONGODB_URI, function(err, database) {
if(err) throw err;
db = database;
// Start the application after the database connection is ready
app.listen(PORT, () => console.log(`Server listening on port ${PORT}`));
});
module.exports = db;
module.exports = app;
routes / api.js:
// const app = require("../server");
var db = require("../server");
const { videos } = require("../helpers/videos");
const { videoLinksValidator } = require("../helpers/video-links-validator");
const { getTitleType } = require("../helpers/title-type-extractor");
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
console.log("RESPONSE", res)
});
// Create a GET route
router.get("/express_backend", (req, res) => {
res.send({ express: "YOUR EXPRESS BACKEND IS CONNECTED TO REACT" });
});
router.post("/video_url", async (req, res)=> {
const videoURL = req.body.videoURL.videoURL;
let titleType = await getTitleType(videoURL);
let videosAndTitle = await videos(videoURL, titleType);
let videoAndTitleReady = await videoLinksValidator(videosAndTitle);
console.log(videoAndTitleReady)
return res.send(videoAndTitleReady);
});
module.exports = router;
答案 0 :(得分:2)
此错误是因为您使用的是res.render。 res.render将尝试渲染您必须设置视图引擎(如玉石等)的页面。但是在这里您不需要这样做,因此将E:\
更改为res.render
试试这个
res.send
对于第二个问题,必须在server.js中将bodyParser置于顶部
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.send('error');//this or res.status(err.status || 500).send('error')
});
答案 1 :(得分:1)
我有同样的错误。我忘记将下面的语法添加到server.js文件中
//set default engine, and provide [handlebars as] extension
app.set('view engine', 'handlebars');
答案 2 :(得分:0)
在您的代码中,您正在渲染,而如果您的视图引擎无法正常工作,则将导致错误
res.status(err.status || 500);
res.render('error');
因此,如果您将应用程序用作API网关,则必须发送这些响应。 res.send当前不推荐使用
res.status(err.status || 500).send('error');