I am trying to pass a value as JSON from my back end to the front end of my application. I am currently running express.js and the connection for all post methods is PERFECT.
Upon a button click in the FRONT-END in my application I want to get back an invoice number from my server.
On the front end my code looks like this with jQuery:
$.get("/invoiceNumber", function(data) {
console.log(data.number);
});
On the back end it looks like this:
app.get("/invoiceNumber", function(req, res) {
res.json({ number: 4 });
});
Currently I am passing 4 just as a test.
The Error I am getting is:
GET http://127.0.0.1:3000/invoiceNumber 404 (Not Found)
If I try to go directly to:
I get:
Cannot GET /invoiceNumber
it looks that this question is duplicated How to allow CORS?
So you can enable all cors requests with
npm install cors
your App.js
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
app.get('/products/:id', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'})
})
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80')
})
you can find additional information here https://expressjs.com/en/resources/middleware/cors.html#enable-cors-for-a-single-route