0
votes

I am trying to create nodejs express application, I am facing weird issue while using routes This is how my server.js looks

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const api = require('./server/routes/api');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));

app.use('/api',api);
app.use(express.static(path.join(__dirname,'dist')));

app.get('/',function(req,res){
res.sendFile(path.join(__dirname,'dist/index.html'));
});

const port = process.env.port || 3000;

app.set('port',port);
const server = http.createServer(app);
server.listen(port,function(){
console.log('server running at port '+port);
});

I am getting following error "node_modules/express/lib/router/index.js:458 throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) "

Please let me know where I am going wrong

1

1 Answers

1
votes

I just forgot to add module.exports = router in my api.js adding this solved my problem