Express router and routes Node.js

Express router and routes Node.js



i get this error when i try to route my



My code


'use strict';

const express = require('express');
const app = express ();
const chatCat = require ('./app');


app.set('port', process.env.PORT || 3000);
app.use(express.static('public'));
app.set('view engine', 'ejs');

app.use('/', chatCat.router);



app.listen(app.get('port'), () =>
console.log('running on port:', app.get('port'));
);



Error


throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
^



i try routing but it keep giving me errors from my express.js





You’re plugging into the router, but you need to plug in a routing function, e.g. (a,b) => ...
– Ashley Frieze
Aug 20 at 11:00




2 Answers
2



Try this code:


var express = require('express');
var app = express();


app.set('port', process.env.PORT || 3000);
app.use(express.static('public'));
app.set('view engine', 'ejs');

var chatCat = require('./app');
app.use('/',chatCat);

app.listen(port,function()
console.log("Server is running on port: "+port);
);





still the same error, i am wondering if its because i installed the old version of express.js
– isaac mathias
Aug 21 at 12:33



Try creating app with this https://expressjs.com/en/starter/generator.html it will create nice structure for your project. As for your error - it needs to be express middleware function. You can't import router from your app.js as it don`t have such property. If you want to have route described here use:


app.method('/yourRoute', function(req, res, next)
//your middleware code
);



also change this:


app.listen(app.get('port'), () =>
console.log('running on port:', app.get('port'));
);



to this:


app.listen(3000);



And you can change 3000 to any unused port you want





am not importing my router from app.js, am importing it from the folder call app that has a file "index.js" in it that has this code "module.exports = { router: require('./routes')()"
– isaac mathias
Aug 21 at 12:36





@isaacmathias this const chatCat = require ('./app'); takes what you exported in app.js file which is located at same directory as file from which you paste your code
– user86168
Aug 21 at 16:12


const chatCat = require ('./app');






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

ᵟᴈ,ᴘᵨᵷᴬ ᴳᵵᴂᴮᵇᵘᴀᴈᴵᵪᵬᴵᴬᴢᵔᵧ,ᵄᴠᴹᵔᴍᵲᵜᴫᵄᵋᴅ,ᵪᵢᵠ ᴡᵗ,ᵷᴝᵲ ᴖᴤᵡ,ᴎ,ᴚ ᵡᵪᵀ,ᴐᵉ,ᵿᴂ,ᴽᴽᵍᵟᵍᴠᵓᵯᴞᵅᵛᵢ,ᴐᴁ ᵺᴉᵸᴵᴶᵄᴪᵷ,ᴌᴠᴗᴚ,ᵟᵺᵳᴝᴉᴰ,ᵹᵥ ᵂᴴ,ᴵ,ᵉᵿ ᴕᵕ,ᴃᴡᴒᵐᴇᴳᵅᵞᴒᴝᴳᴋᴗᵢᵶᵢᵅᴣᴑᵘᵷᵾᴍᴔᴵ,ᴢᴘ,ᴮᵫᴘ,ᵳ,ᴩᵓᴞ