我是Node.js的新手,我不知道如何解决此错误:
D:\Uni\Projects\Bulanovm\node_modules\koa\lib\application.js:121
if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
^
TypeError: middleware must be a function!
at Application.use (D:\Uni\Projects\Bulanovm\node_modules\koa\lib\application.js:121:41)
at Object.<anonymous> (D:\Uni\Projects\Bulanovm\index.js:45:5)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
我正在寻找解决方案,但找不到任何可以帮助我的东西。这是我要运行的代码:
'use strict'
const Koa = require('koa')
const Router = require('koa-router')
const views = require('koa-views')
const staticDir = require('koa-static')
const bodyParser = require('koa-bodyparser')
const koaBody = require('koa-body')({multipart: true, uploadDir: '.'})
const session = require('koa-session')
const sqlite = require('sqlite-async')
const bcrypt = require('bcrypt-promise')
const fs = require('fs-extra')
const mime = require('mime-types')
const accounts = require('./modules/accounts')
const getadminlogin = require('./routes/admin/get/login')
const getadminfrontpage = require('./routes/admin/get/frontpage')
const getadminregister = require('./routes/admin/get/register')
const getadminlogout = require('./routes/admin/get/logout')
const postadminlogin = require('./routes/admin/post/login')
const postadminregister = require('./routes/admin/post/register')
const postadminqoute = require('./routes/admin/post/quote')
const getuserlogin = require('./routes/user/get/login')
const getuserfrontpage = require('./routes/user/get/frontpage')
const getuserregister = require('./routes/user/get/register')
const getuserlogout = require ('./routes/user/get/logout')
const getuserproblem = require ('./routes/user/get/problem')
const postuserlogin = require ('./routes/user/post/login')
const postuserregister = require ('./routes/user/post/register')
const postuserproblem = require ('./routes/user/post/problem')
const app = new Koa()
const router = new Router()
app.keys = ['darkSecret']
app.use(staticDir('public'))
app.use(bodyParser())
app.use(session(app))
app.use(views(`${__dirname}/views`, { extension: 'handlebars' }, {map: { handlebars: 'handlebars' }}))
app.use(getadminlogin) //line 45
app.use(getadminfrontpage)
app.use(getadminregister)
app.use(getadminlogout)
app.use(getuserfrontpage)
app.use(getuserlogin)
app.use(getuserregister)
app.use(getuserlogout)
app.use(getuserproblem)
app.use(postadminlogin)
app.use(postadminregister)
app.use(postadminqoute)
app.use(postuserlogin)
app.use(postuserregister)
app.use(postuserproblem)
const port = 8080
const saltRounds = 10
/**
@name Home
@route
@authentication
*/
app.use(router.routes())
module.exports = app.listen(port, async() => {
const db = await sqlite.open('./website.db')
await db.run('CREATE TABLE IF NOT EXISTS tech (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, pass TEXT);')
await db.run('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, pass TEXT);')
await db.run(`CREATE TABLE IF NOT EXISTS repairs (id INTEGER PRIMARY KEY AUTOINCREMENT,
applianceType TEXT, years INEGER, manufacturer TEXT, description TEXT,
addedBy TEXT, completed INTEGER, techquote INTEGER);`)
await db.close()
console.log(`listening on port ${port}`)
})
(D:\ Uni \ Projects \ Bulanovm \ index.js:45:5)第45行是app.use(getadminlogin) 任何帮助和建议,将不胜感激。非常感谢。
答案 0 :(得分:0)
请给出你的中间件代码,现在我给出一个解决方案。由于没有代码,我不知道这是否可能。 在您的中间件中
module.exports = () => {
return getadminlogin // return your middleware code
}