• Register
0 votes
1.7k views

Problem :

I am just starting coding using the nodejs with express. So I simply did this in my file test.js which is into my folder routes :

const myexpress = require('express');
const myrouter = new myexpress.Router();
myrouter.get('/test', (req, res) => {
res.send(`I am the test`);
});
module.exports = myrouter;

Then, inside my server.js :

const mytest = require('./server/routes/test');
app.use('/test',mytest);

But in the browser, it tells me it Cannot get/test

I don't understand why this is happening to me. Please let me know how can I fix this?

7 5 2
3,870 points

1 Answer

0 votes

Solution :

A problem seems to be in how you are mounting your router. Looking at a router middleware API it seems you should be doing it as shown below.

test.js

const myexpress = require('express');
const myrouter = new myexpress.Router();
myrouter.get('/test', (req, res, next) => {
  res.send("I am the test");
  next();
});
module.exports = myrouter;

server.js

const myexpress = require('express');
const myapp = myexpress();
const mytest = require('./test'); 
app.use('/', mytest);
app.listen(3000);

OR

Below is the very simple fix that should resolve all your issues.

You need to Replace below code

const router = new express.Router();

With the following code.

const router = express.Router();

I hope you will be able to fix your error with the help of above given solutions.

9 7 4
38,600 points

Related questions

0 votes
2 answers 1.9K views
1.9K views
Problem : Facing following express issue throw new typeerror('router.use() requires a middleware function but got a ' + gettype(fn))
asked Nov 18, 2019 peterlaw 6.9k points
0 votes
0 answers 46 views
46 views
TypeError: Router.use() requires a middleware function but got a Object at Function.use (E:\firebase-CRUD_api\functions\node_modules\express\lib\router\index.js:458:13) at Function.<anonymous> (E:\firebase-CRUD_api\functions\node_modules\express\lib\application.js:220:21) ... at Function.Module._load (internal/modules/cjs/loader.js:899:14) at Module.require (internal/modules/cjs/loader.js:1042:19)
asked May 17 anonymous
1 vote
2 answers 412 views
412 views
Problem : I am very new to the Node.js and I am having few issues. I am using the Node.js 4.10 and Express 2.4.3. While trying to execute my code I am receiving below error : Error: Can't render headers after they are sent to the client. ... , scope: 'offline_access,email,user_about_me,user_activities,manage_pages,publish_stream', failedUri: '/noauth' }) ])); realapp.use(realapp.router); }); . . .
asked Dec 12, 2019 alecxe 7.5k points
0 votes
1 answer 140 views
140 views
Problem : I have recently updated the node to 7.2.1 and noticed that there is the warning coming as below : (node:4346) DeprecationWarning: Calling an asynchronous function without callback is deprecated. Why is this 4346 ? I only have 2000 lines of code in the js file, so it must not be the line-number. Where should I look for that code?
asked Dec 10, 2019 alecxe 7.5k points