Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

919
Views
nodejs express no puede obtener la URL y los datos del servidor del nodo

Escribir una aplicación simple para obtener datos del servidor nodejs.

pero parece que no obtiene el archivo en el navegador ni en POSTMAN. He intentado varias veces para comprobar las direcciones URL, pero fue en vano.

aquí están mis archivos:

aplicación.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}'); })

rutas.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;

Menú.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 va así

 [ { "_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" } ]

el error es No se puede OBTENER /getMenu

no estoy seguro de dónde está el error porque es una ruta simple... no estoy seguro de lo que falta. Gracias

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Su código debería funcionar con http://localhost:8055/getMenu. Copié tu código y me funciona el enlace al repositorio .

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

0

El error Cannot GET / surge al intentar visitar http://localhost:8055

  • Se genera porque no hay una ruta para el directorio raíz http://localhost:8055/ usando la ruta / en express.

  • Hay una ruta para http://localhost:8055/getMenu que funciona bien.

código de prueba

 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 Report

0

Parece que ha habido un error técnico.

Está funcionando bien ahora. Acabo de reiniciar nodemon app.js.

y está funcionando ahora. Estuve intentando todo este tiempo arreglar la URL.

gracias por su ayuda a todos

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!