I'm new to Node.js. What is the error and how to fix it?Help me please Error code + below is my code:
app.use(fn, '/api/auth', require('./routes/auth.routes'))
ReferenceError: fn is not defined
at Object.<anonymous> (D:\Apps\app.js:6:9)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
const express = require('express')
const config = require('config')
const mongoose = require('mongoose')
const app = express()
app.use(fn, '/api/auth', require('./routes/auth.routes'))
const PORT = config.get('port') || 5000
async function start() {
try{
await mongoose.connect(config.get ('mongoUri'), {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
app.listen(PORT, () => console.log('App worked'))
} catch (e){
console.log('Server Error', e.message)
process.exit(code, 1)
}
}
start()
Tried it app.use(fn: '/api/auth', require('./routes/auth.routes')), but it doesn't work
A proper call to app.use()
takes a first optional argument that is a string which represents a path or a path prefix and then it takes any number of middleware function references after that. Doc reference for app.use()
here .
So, what you are doing:
app.use(fn, '/api/auth', require('./routes/auth.routes'))
Is not a valid way to call app.use()
and then on top of that, fn
is not even defined which makes it a ReferenceError (won't even run).
I don't know where you got the idea to use the form you have, but it should probably be this:
app.use('/api/auth', require('./routes/auth.routes'))
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.