Aet460
December 7, 2022, 8:13pm
1
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
If you can supply a link to your full project code, that would be helpful, so we can test things out.
Aet460
December 7, 2022, 8:27pm
3
How may i do that ? sending my replit link or something ?
Aet460
December 7, 2022, 8:31pm
5
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.
If you use app.use('/london', londonIndex);
then inside the route file it is just /
otherwise you get a /london/london
route.
The function needs to be taken off the object const { londonGetter } = require("../Controllers/London.js");
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)
}
Aet460
December 8, 2022, 11:47pm
7
Will Try out in a few minutes thank you so much.
You are A Legend
Aet460
December 11, 2022, 7:41pm
8
Tried out but still error made everything as u say now getting /london but no response error function londonGetter not a function
lasjorg
December 11, 2022, 7:43pm
9
Make sure it is the actual function and not the object you are using. See point #2
lasjorg
December 11, 2022, 7:44pm
11
I would suggest you log it out after the require.
Aet460
December 11, 2022, 7:45pm
12
Wdym by log it out log what out? can u show example please ?