Back End Development and APIs Projects - URL Shortener Microservice

Tell us what’s happening:

Describe your issue in detail here.
In the below code i have this snippet of code
"app.post(‘/api/shorturl’, function(req, res) {
res.json({message:‘Success’})
}) "

when posting the url it is showing as “Cannot GET /api/shorturl” in the replit I don’t understand why

Your code so far

require(‘dotenv’).config();
const express = require(‘express’);
const cors = require(‘cors’);
const app = express();
const bodyParser = require(‘body-parser’); // Corrected import statement

// Basic Configuration
const port = process.env.PORT || 3000;

app.use(cors());
app.use(bodyParser.urlencoded({ extended: false })); // Corrected usage

app.use(‘/public’, express.static(${process.cwd()}/public));

app.get(‘/’, function(req, res) {
res.sendFile(process.cwd() + ‘/views/index.html’);
});

// Your first API endpoint
app.get(‘/api/hello’, function(req, res) {
res.json({ greeting: ‘hello API’ });
});

app.post(‘/api/shorturl’, function(req, res) {
res.json({message:‘Success’})
})

app.listen(port, function() {
console.log(Listening on port ${port});
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Back End Development and APIs Projects - URL Shortener Microservice

1 Like

You are trying to access the /api/shorturl endpoint using a GET request, but you have only defined a POST request for that endpoint in your code.

app.get('/api/shorturl', function(req, res) {
  // Add your GET request 
  res.json({message: 'This is a GET request to /api/shorturl'});
});

thanks for the reply bro!!!

1 Like

It looks like there might be an issue with the way you’re testing the /api/shorturl endpoint. The error “Cannot GET /api/shorturl” usually indicates that you are trying to access the endpoint using a GET request instead of a POST request.

In your code, the /api/shorturl endpoint is defined as a POST endpoint, so you should be making a POST request to it. If you are trying to test it from a web browser by entering the URL, the browser will send a GET request by default.

To test your endpoint, you can use a tool like Postman or curl, or you can modify your code to handle both GET and POST requests for that endpoint. For example:

app.route('/api/shorturl')
  .get(function(req, res) {
    res.json({message: 'GET request to /api/shorturl'});
  })
  .post(function(req, res) {
    res.json({message: 'Success'});
  });

This modification uses the app.route method to define the /api/shorturl endpoint for both GET and POST requests. Now, if you access it from a web browser, you should see a response for the GET request as well.

Make sure you are testing the POST request properly, and the “Cannot GET /api/shorturl” error should no longer appear for that endpoint.

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