Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Browser showing "Cannot GET /api/posts/ " instead of "hello" from res.send('hello')

My index.js file:

//Dependencies
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const posts = require('./routes/api/posts.js');

//Configuration
const port = process.env.PORT || 5000;

//App object
const app = express();

//Middleware
app.use(bodyParser.json());
app.use(cors());

//Main app
app.use('api/posts',posts);

//Starting server
app.listen(port,()=>{
  console.log(`server running at ${port}`);
});

My Api file:

//Dependencies
const express = require('express');
const mongodb = require('mongodb');

//Mini app
const router = express.Router();

//Get post
router.get('/',(req,res)=>{
  res.send('hello');
});

//Add post

//Delete post

module.exports = router;

I’m expecting to get "hello" in my browser but constantly getting "Cannot GET /api/posts/" in firefox and postman. What should I do now?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Correction :-

//Main app
app.use('/api/posts',posts);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading