My response not works Express Error: Cannot Get london {edited}

I have Some problems i did create 2 folders in express using replit

have server.js file which has these codes in it

const express = require("express");
const port = process.env.PORT || 3000;
const hostname = "127.0.0.1";
const index = require("./routes/index");
const londonIndex = require('./routes/londonIndex');
const fs = require('fs');
const app = express();
const cors = require('cors');

app.use(cors());

app.use('/', index);
app.use('/london', londonIndex);
app.listen(port, () => {
  console.log(
    `API SERVER IS RUNNING ON ${hostname} host and  listening on ${port} port successfully!`
  );
});

and have 2 folder one is routes the another one is Controllers

in routes have 2 files index and londonIndex

index code :

const express = require("express");

var router = express.Router();

router.get("/", (req, res) => {
  res.send("Welcome to My Api");
});

module.exports = router;

londonIndex :

const express = require("express");
const londonGetter = require("../Controllers/London.js");

var router = express.Router();

router.get("/london", (req, res) => {
  londonGetter();
});

module.exports = router; 

and have one controller in Controllers folder which is London.js code :

const axios = require("axios");

module.exports.londonGetter = async () => {
  try {
    const { data: response } = await axios.get('sample url')
    return response
  }

  catch (error) {
    console.error(error);
  }

}

and last this is image of files and folders i talk about

Controllers-London-js-Triall-Replit

How may i do that ? sending my replit link or something ?

I think this link may work Ali-ErenEren12 (Ali Eren Tabak) - Replit here Triall file is my fullcode project you may see it i think or if u access to my profile you may go into Trials folder in my profile then in that folder Triall node file my project file that i display codes here

Few things.

  1. If you use app.use('/london', londonIndex); then inside the route file it is just / otherwise you get a /london/london route.

  2. The function needs to be taken off the object const { londonGetter } = require("../Controllers/London.js");

  3. You have to res the response. You can do it a few different way.

I might just pass the londonGetter controller to the route

router.get("/", londonGetter);

Then inside the controller add the req and res parameters and res the response you get back (I shortened it for brevity).

module.exports.londonGetter = async (req, res) => {
  const response = await axios.get('API')
  return res.json(response)
}

Will Try out in a few minutes thank you so much.

You are A Legend :clap:

Tried out but still error made everything as u say now getting /london but no response error function londonGetter not a function

Make sure it is the actual function and not the object you are using. See point #2

yes it is


I would suggest you log it out after the require.

Wdym by log it out log what out? can u show example please ?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.