URL Shortener Microservice - PROBLEM - strict-origin-when-cross-origin

Tell us what’s happening:
Hi im having some troubles realizing the POST request via web explorer.
Im getting an ERR_CONNECTION_REFUSED error and if look at the network in the devTools the problem seems to be:
strict-origin-when-cross-origin

On the other hand it works perfectly fine in the POSTMAN,
since im a new user cant post more than 1 image so the postman setup is a
Post method with the next body:
{
“url”: “https:// www. google. com/”
}
and i receive the response:
{
“original_url”: “https:// www. google .com/”,
“short_url”: 1
}

which means that it actually works

with all in count i think its a problem with cors so i expended a few evening trying to solve it, i realized multiple cors config like this:

app.use(
	cors({
		origin: '*',
		methods: 'POST, GET',
		// credentials: true,
		// allowedHeaders: 'Content-Type',
		// preflightContinue: false,
	})
)

And

app.use((req, res, next) => {

res.header( 'Access-Control-Allow-Origin' , '*' );

res.header( 'Access-Control-Allow-Headers' , 'Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Request-Method' );

res.header( 'Access-Control-Allow-Methods' , 'GET, POST, OPTIONS, PUT, DELETE' );

res.header( 'Allow' , 'GET, POST, OPTIONS, PUT, DELETE' );

next();

});

and multiple combination of those, changing the origin to true, ‘*’, false, testing around different headers parameters, etc. But nothing seems to work.

i tried everything i could think of but since i cant solve it im reccuring to your help.

sorry if any sentence doesnt make sense, english is not my first language.

Thank you so much for your time!

Cheers!

Your code so far
Since the project is splitted in multiple folders/files here is the link to the repo:
gh: /AgustinCatanzaro/url-shortener

my call is that the problem is in the index.js so here it is if you dont want to go the github

index.js:


require('dotenv').config()

const express = require('express')

const cors = require('cors')

const app = express()

// connectDB

const connectDB = require('./db/connect')

//middleware

const notFoundMiddleware = require('./middleware/not-found')

const errorHandlerMiddleware = require('./middleware/error-handler')

//init

app.use(express.json())

app.use(

cors({

origin: '*',

methods: 'POST, GET',

// credentials: true,

// allowedHeaders: 'Content-Type',

// preflightContinue: false,

})

)

// app.set('trust proxy', 1)

app.use('/public', express.static(`${process.cwd()}/public`))

//routers

const urlShortenerRouter = require('./routes/url-shortener')

//routes

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.use('/api/shorturl', urlShortenerRouter)

//error handlers

app.use(notFoundMiddleware)

app.use(errorHandlerMiddleware)

// Basic Configuration

const port = process.env.PORT || 3000

//added db connection

const start = async () => {

try {

await connectDB(process.env.MONGO_URI)

app.listen(port, () =>

console.log(`Server is listening on port ${port}...`)

)

} catch (error) {

console.log(error)

}

}

start()

Your browser information:

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

Challenge: Back End Development and APIs Projects - URL Shortener Microservice

Link to the challenge:

hello and welcome to fcc forum :slight_smile:

  • share that “repl” link instead, thats more interactive

happy learning :slight_smile:

It is not a CORS error.

You are using the wrong built-in middleware. You want express.urlencoded()

Or as you have removed the body-parser middleware from the boilerplate add that back and use it as the curriculum teaches.

Your repo

https://github.com/AgustinCatanzaro/url-shortener

Hi lasjorg,
Thank you so much for taking the time and helping me with the issue, Your solution worked perfectly, i didnt even notice that i erased the body-parser dependency. With that and the express.urlencoded() now it works as a charm.

Thank you very much!

Sincerely, Agustin!