2

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:

http://127.0.0.1:3000/invoiceNumber

I get:

Cannot GET /invoiceNumber
Pok3r Princ3
  • 147
  • 2
  • 13

2 Answers2

1

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

enter image description here enter image description here

Ishikawa Yoshi
  • 1,779
  • 8
  • 22
  • 43
  • I've installed & implemented the cors module, but i still get the same error. Any other ideas ? – Pok3r Princ3 Nov 23 '18 at 11:04
  • 'Access to XMLHttpRequest at 'localhost:3000/invoiceNumber' from origin 'http://127.0.0.1:3000' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.' this error 100% means that problem in cors – Ishikawa Yoshi Nov 23 '18 at 11:12
  • so you can see on screenshot that this should works, you need to check your code. Or post it here and we check – Ishikawa Yoshi Nov 23 '18 at 11:39
  • @Pok3rPrinc3 feel free to ask if you need any additional help – Ishikawa Yoshi Nov 23 '18 at 11:59
  • Thanks for the time that you've put in helping me out Ishikawa, i'm trying to do all this to basically achieve this functionality: I'm using `express.js` and i have a server.js file in the root dir. of my project, and i have a public folder that i serve to the user's browser. I want to create a file in the root dir similar to the server.js (to be able to require NPM modules) that i can access from another file, in the public folder. If i try to do that right now i always get `GET http://127.0.0.1:3000/invoices.js 404 (Not Found)` I've tried ajax .get, plain .get, fetch full path, etc... – Pok3r Princ3 Nov 23 '18 at 12:37
  • I'm not sure if my approach is correct (or even possible), so i'm open to ideas. – Pok3r Princ3 Nov 23 '18 at 12:39
  • if i understand your problem correct, you talk about import/export module check this link https://adrianmejia.com/blog/2016/08/12/getting-started-with-node-js-modules-require-exports-imports-npm-and-beyond/#Module-exports-vs-Exports – Ishikawa Yoshi Nov 23 '18 at 12:54
  • Thanks for this resource, i'll check it out! Also, thanks again for taking the time to help me out. – Pok3r Princ3 Nov 23 '18 at 12:57
0

Looks to me like you need to refrence where you back end is running from:

$.get("/invoiceNumber", function(data) {
  console.log(data.number);
});

if your express app is running on port 3000, change your front end to the following:

$.get("localhost:3000/invoiceNumber", function(data) {
  console.log(data.number);
});
Luke Walker
  • 511
  • 5
  • 19
  • This allowed me to access the `"localhost:3000/invoiceNumber"` path in the browser, but i'm getting a `Access to XMLHttpRequest at 'localhost:3000/invoiceNumber' from origin 'http://127.0.0.1:3000' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.` I'm also using Allow-Control-Allow-Origin: * to work around the Chrome restriction on localhost. – Pok3r Princ3 Nov 23 '18 at 09:58
  • Yes, Cors is a cross origin restiction, if your on chrome theres a chrome extention called "Moesif CORS" which will disable CORS for you. – Luke Walker Nov 23 '18 at 10:04