Syntax error in loopback api gateway - node.js

I am following a tutorial :http://strongloop.com/strongblog/node-js-loopback-api-gateway-sample-applications/
and I am getting an error in the code underneath the Proxy heading in the link mentioned above.
Code against the error is
var proxy = require('./middleware/proxy');
var proxyOptions = require('./middleware/proxy/config.json');
app.use(proxy(proxyOptions));
{
"rules": [
"^/api/(.*)$ http://localhost:3002/api/$1 [P]"
]
}
the error message is
is there any thing wrong with the json?

It looks like you just copied and pasted the code from that blog post, but that was not intended to be runnable code, it was just a snippet to illustrate that you could proxy requests. I think the blog post could be a lot more clear (I'll ping the author of it separately). That said, we have an example project which has some actual code that shows you how you might proxy actual requests.
If you're just trying to get a basic example working, then you probably don't even need those lines in your application JS file, so I'd just remove them entirely.
If you do need to proxy requests, take a look at the example project I linked to above, specifically the proxy middleware they have in it.

My Problem is solved the problem was that I did not run the API Server http://localhost:3002 where the request was to be sent. I was only running the proxy server whereas there should be an API Server running also to which the request was to be sent.

Related

axios network error with cors activated still returns error

the link to the code link
I am using axios and nodejs.
All routes work and give a response except one. which returns Network error.
that route is /api/ads/myads.
The route works on its own but when used with redux actions it doesn't
The network tab says that this request was blocked.
So I tried to add cors but that didn't solve the issue.
The file in question is .../actions/adActions this one is producing the error while others don't
I will be uploading the code in a minute
This turned out to be my adblocker thinking this was actual ads

Following a Twilio Tutorial to Record Phone Calls: Resulting in a 404 HTTP Error

I'm following this tutorial linked here which incorporates Twilio Programmable Voices and Express to record inbound phone calls. I followed the tutorial exactly, set up a ngrok public server, connected it to my http://localhost:3000, and I get the following output when I navigate to my ngrok link:
Cannot GET /
When I check ngrok in my terminal, it says "404 Not Found" for a GET (which is not in my code) and POST request. If I add a simple GET request to my code (such as "Hello World"), then in the browser, I see "Hello World" when I run my application. In the terminal, GET is able to succeed but POST still returns a "404 Not Found".
I've checked several other stack overflow questions, I've tried importing body-parser as recommended and adding these two lines:
const bodyParser = require('body-parser');
app.use(bodyParser.json());
For reference, my directory has a record.js file, package.json and package-lock.json files, and necessary node modules. In my Twilio Console, I did set up a web hook for my Twilio # to the ngrok link which looks like this: http://39b62a0c34b1.ngrok.io. To show my order of executions:
Run ./ngrok http 3000 in Mac Terminal
Run node record.js in VS Code terminal
Navigate to http://39b62a0c34b1.ngrok.io
Call my Twilio #
The voice message tells me that there's a problem with my application. I've checked the Twilio Debugger and I receive back a 11200 Error informing me that my ngrok link returned a "HTTP status code of 404".
I'm quite new to Node.js, Express, and Twilio, so any and all help is appreciated (please simplify your explanation)! I want to be a Developer Evangelist for Twilio someday so I'm more than prepared to make mistakes now to troubleshoot them in the future.
Twilio uses x-www-form-urlencoded, not JSON, for Webhhooks.
You will need to add Express middleware to parse urlencoded data out.
The 3rd Line:
// Body Parser Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
Where do I find the specifications for inbound HTTP requests to my server?
Let me know how it goes.
Still not clear why you are getting a 404 - Not Found. The app.post should kick in for POST operations. app.all for all operations. Are you using the /record path in your testing?

Connecting front-end and back-end with react and mongo DB - 404 error

I am trying to connect an app hosted in my localhost:3000 port. The back-end is on the localhost:8080 port (not a remote API but on my own PC. I downloaded a pre-created back-end api and linked the front-end to the back-end with MongoDB using an .env file.
The weird thing is that on MongoDB the connection looks ok, following the tutorial I am using. The backend and the front-end also look alrigh, however, I am unable to login with the form in the screenshot.The error I get when trying to login or create a new user is "xhr.js:178 POST http://localhost:3000/login 404 (Not Found)"
It was a bit hard to put the whole code here, so I am linking you to the front-end repo: https://github.com/manuelalonge/complex-app and the back-end repo: https://github.com/manuelalonge/back-end-api
I can understand the issue is most likely on the back-end but I could not understand where exactly. I tried to get back to the previous lessons in the tutorial but it still doesn't get solved.
Probably it is easier to solve this issue with a screenshare session, so if anybody would contact me I'll be grateful.
Screenshot --> [1]: https://i.stack.imgur.com/jVJzn.png
Your screenshot points to what's wrong. You're posting to localhost:3000, as you know this is the frontend webpack server.
You'll want to create an axios config file and set a base url to hit the correct endpoint.
const axiosInstance = axios.create({
baseURL: 'localhost:8080'
});
export default axiosInstance;
Also, please add some sort of file structure.

Unable to get redirect response from Node server using res.writeHead() and .end()

I'm writing a server in Node.js. I've been using the send-data/json package to send responses, with much success. For example, at the end of my API call I use this code:
sendJson(res, res, {some: content})
This works great. However, I have a need to implement a URL redirect in one of my API endpoints. The code I am seeing everywhere to do this looks like this:
res.writeHead(302, {Location: 'http://myUrl.com'})
res.end()
However, this change causes my server to stop sending responses on this endpoint.
What am I missing?
Note: this is vanilla Node without Express.
Update: to clarify, based on contributions in the comments, this is the result of a GET request, so I expect that redirects should be enabled by default. I am still curious why no response is coming back from the server at all, regardless of whether it is an erroneous redirect attempt or a successful one.

Cannot find the file URL using xhr.open('post',url,true) - Status: 404 node js

Trying to Upload an image by XMLHttpRequest(), have issue understanding what is the correct URL to access file via xhr.open(...).
Following this example for server side code and everything..
var xhr = new XMLHttpRequest();
xhr.open('post','../../../../server/routes/saveImage.js',true);
xhr.onload = function() {
if(this.status ==200) {
resolve(this.response);
} else {
reject (this.statusText);
}
};
The Project directory is something like this
Project
client
app
component
product
addproduct.js <-- xhr.open is called from here
server
routes
saveImage.js <-- File being called
Also regarding paths let me know if there is a more convenient way to check the access path or absolute path to use in url.
I think there is a conceptual problem in many levels.
First, When you are XHRing(ajax) an url that means you are accessing the url from CLIENT SIDE. So, Let's say you have an app and HTTP posting or getting an url. Do you have that file from client side? The answer is obviously NO.
let's say you are hosting the app in:
http://localhost/myapps/app
So, When you access ./someFile.txt, ../someFile.txt and ../../someFile.txt you are actually requesting
./someFile.txt-> http://localhost/myapps/app/someFile.txt
../someFile.txt-> http://localhost/myapps/someFile.txt
../../someFile.txt-> http://localhost/someFile.txt
Now, For your problem. You need to host the Server Side upload code somewhere. The example assumes the Server Side code is hosted in, for example, http://localhost/upload and use xhr.open('post','/upload',true);
You need to understand requireing or importing or fs.readFile a file is accessing the path internally. But when you host the app, any client side code like Ajax(XHR) is accessing the url from outside.
The problem indeed was with setting up a server side route, this was mostly clarified, thanks to the answer from #Arkita and comment from #Kasper. But I went ahead to dig for a solution, which may not be very useful to others as this was a dumb question in the first place, but here it goes..
On client side
xhr.open('post','/saveImage/save',true);
on server side if you are using Express.js or other connect based frameworks
app.post('/saveImage/save',(req,res,next)=> {....})
Also the example I linked above may be outdated, this seems more helpful.

Resources