Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

916
Visualizações
nodejs express cannot get the url and data from node server

Writing simple app to fetch data from nodejs server.

but it seems not to get the file in browser as well as in POSTMAN. I have tried multiple times to check the urls but to no avail.

here are my files:

app.js

const express = require('express');
const bodyParser = require('body-parser');
const router = require('./Routes/routes');

const hostname = "localhost";
const port = "8055";

const app = express();

app.use(bodyParser.json());

// CORS
 app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods','GET,POST,PUT,PATCH,DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    next();
    });

    app.use('/', router);
    app.listen(port,hostname, () => {
    console.log('Server is running on http://${hostname}:${port}');
    })

routes.js

    const express = require('express')

var CityListController = require('../Controllers/City')
var RestaurantCityController = require('../Controllers/Restaurants')
var MenuController = require('../Controllers/Menu')


const router = express.Router();

router.get('/getCityList',CityListController.getCityList);
router.get('/getRestaurantsByCity/:cityname', RestaurantCityController.getRestaurantsByCity);
router.get('/getMenu', MenuController.getMenu);

module.exports = router;

Menu.js

const mealtypes = require('../Models/menu.json');

exports.getMenu = (req, res) => {
const resultname = mealtypes.map(items => items.name)

res.status(200).json({
    message: "widgets data fetched successfully",
    name : resultname
 
   })
 }

menu.json goes like this

[
{
    "_id": "1",
    "name": "Breakfast",
    "content": "Start your day with exclusive breakfast options",
    "image": "assets/breakfast.png"
},
{
    "_id": "2",
    "name": "Lunch",
    "content": "Start your day with exclusive breakfast options",
    "image": "assets/lunch.png"
}
 ]

error is Cannot GET /getMenu

not sure where the error is because it is a simple route.. not sure what is missing. thanks

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Your code should work with http://localhost:8055/getMenu. I copied your code and works for me link to repo.

{
    "message": "widgets data fetched successfully",
    "name": [
        "Breakfast",
        "Lunch"
    ]
}
over 4 years ago · Santiago Trujillo Relatório

0

The error Cannot GET / arises from trying to visit http://localhost:8055

  • It is generated because there is no route for the root directory http://localhost:8055/ using the path / in express.

  • The is a route for http://localhost:8055/getMenu which works fine.

Test code

const express = require('express');

const getMenu = (req,res) => res.status(200).json({ test_data: "test_data"});
const router = express.Router();
router.get('/getMenu', getMenu);

const hostname = "localhost";
const port = "8055";

const app = express();
app.use('/', router);
app.listen(port,hostname, () => {
    console.log(`Server is running on http://${hostname}:${port}`);
})
over 4 years ago · Santiago Trujillo Relatório

0

It seems there has been a technical error.

It is working fine now. I just restarted nodemon app.js.

and it is working now. Been trying this whole time to get the url fixed.

thanks for your help all

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda