简体   繁体   中英

TypeError('app.use() requires a middleware function')

enter image description here

here's the app.js(--please find attached image):

 //here's the app.js const express = require("express"); const app = express(); app.use(express.json()); const morgan = require("morgan"); const mongoose = require("mongoose"); const cors = require("cors"); require("dotenv/config"); const authJwt = require("./helpers/jwt"); const errorHandler = require("./helpers/error-handler"); app.use(cors()); app.options('*', cors()); //middleware app.use(morgan("tiny")); app.use(authJwt()); app.use(errorHandler()); //-> **this isn't correct?** //Importing routing of products const categoriesRoutes = require("./routes/categories"); const productsRoutes = require("./routes/products"); const usersRoutes = require("./routes/users"); const ordersRoutes = require("./routes/orders"); const req = require("express/lib/request"); const res = require("express/lib/response"); const api = process.env.API_URL; //routers app.use(`${api}/products`, productsRoutes); app.use(`${api}/categories`, categoriesRoutes); app.use(`${api}/orders`, ordersRoutes); app.use(`${api}/users`, usersRoutes); //Database mongoose.connect(process.env.CONNECTION_STRING, { useNewurlParser: true, useUnifiedTopology: true, dbName: "eshop-database", }).then(() => { console.log("database connection is ready"); }).catch((err) => { console.log(err); }); //Server app.listen(3000, () => { console.log("Server is Running http://http://localhost:3000"); });
//error image

 //here's the error handler code function errorHandler(err, req, res, next){ if (err) { res.status(500).json({message: err}) } } module.exports = errorHandler; > Blockquote (--please find attached image) here's the error C:\Users\steve\Backend\node_modules\express\lib\application.js:210 throw new TypeError('app.use() requires a middleware function') ^ TypeError: app.use() requires a middleware function at Function.use (C:\Users\steve\Backend\node_modules\express\lib\application.js:210:11) at Object.<anonymous> (C:\Users\steve\Backend\app.js:17:5) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47

Your error handler function is correct, but the problem is that you're calling the function instead of passing it directly to your express app.

Remove the parenthesis after the errorHandler and it should work.

app.use(errorHandler);  // Don't call errorHandler, express will call it

Think of it like this.

if I just call errorHandler() in any context it won't return anything.

That means that errorHandler() evaluates to undefined .

Now in your code, when you do app.use(errorHandler()) instead of evaluating to the following:

app.use(function(err, req, res, next){
    if (err) {
        res.status(500).json({message: err})
    }
});

Your code is actually evaluating to

app.use(undefined);

and so express is throwing an error because undefined is not a function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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