0

I need help i am just send my server.js as a parameter to my route.js file so that i made a line in server .js file it shown that require is not a function.

var express = require('express');

var app = express();

app.set('view engine','ejs');
app.set('views', __dirname + '/../public/views');

app.use(express.static(__dirname + '/../public'));

require('./routes.js')(app) // in this line error occurs

module.exports = app;

route.js

module.export = function(app){
app.get('/', function(req, res){
res.send('You are in home page');
});
app.get('/top', function(req, res){
res.send('You are in top page');
});
app.get('/results', function(req, res){
res.send('You are in results page');
});
app.get('/api/results', function(req, res){
res.json({message: 'API page'});
});

}
  • Thats not an appropriate syntax please refer here https://stackoverflow.com/questions/23923365/how-to-separate-routes-on-node-js-and-express-4 – Yatin Gaikwad Mar 27 '20 at 14:20
  • 1
    It doesn't say `require is not a function`, it says `require('./routes.js') is not a function`. This means in `routes.js` you're not exporting a function, but something else. Please show `routes.js` so that we may identify the problem with the export. – Klaycon Mar 27 '20 at 14:20
  • @YatinGaikwad The syntax is fine. Your linked question merely shows another approach that can be used, but the correct solution to a problem with an implementation isn't to completely ditch that approach and copy another. – Klaycon Mar 27 '20 at 14:23
  • @YatinGaikwad since i am a beginner can you explain what can i do? – karthikeyan S Mar 27 '20 at 14:24
  • @Klaycon i have updated route js help me – karthikeyan S Mar 27 '20 at 14:28
  • @karthikeyanS `module.export` --> `module.exports` – Klaycon Mar 27 '20 at 14:28
  • @Klaycon Thank you so much its working good. – karthikeyan S Mar 27 '20 at 14:32

0 Answers0