I am getting an error saying "Failed to load resource: net::ERR_CONNECTION_REFUSED... delete.js" when I try to GET '/' via my express server. This generally happens when I try to submit a form from http://localhost:3000/add and a redirect is made to '/' using res.redirect('/')
. here is the code in question where the redirect is being made:
app.post('/add', (req, res) => {
const products = JSON.parse(
fs.readFileSync(`${__dirname}/products.json`, 'utf-8'),
);
const newProduct = req.body;
newProduct.id = createProductId();
newProduct.organic = !!newProduct.organic;
products.push(newProduct);
fs.writeFileSync(
`${__dirname}/products.json`,
JSON.stringify(products, null, 2),
'utf-8',
);
return res.redirect('/');
});
and here is the HTML file where delete.js
is being loaded:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Products</title>
</head>
<body>
<div class="all-products">
{%PRODUCT_CARDS%}
</div>
<a href="/add">
<button type="submit" class="btn btn-primary">Add Product</button>
</a>
<script src="js/delete.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
</body>
</html>
Any help will be much appreciated Thanks
If add a proxy to the root "/", then when your page loads and the browser request for delete.js, instead of node serving the files, it redirects that request too.
it is better to proxy sub-urls like '/api', so if you want to make a request from the frontend, you do app.post('/api/add...')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.