Express API response directly outputted in browser instead being handled by Nuxt.js - node.js

I have an express server that is connected to mongodb. I have a nuxt route for /account under pages directory. I also have a same route in the express server app.use('/account', account) which is handling the requests.
The database model has been set correctly and the response is sent by the server using return res.status(200).json(User) whenever the route is called upon using a get request. I'm also seeing the response being delivered to the browser.
But the problem is the response is being outputted directly in the browser as raw json. Nuxt is not being loaded to handle the response. I haven't setup any $axios data call in the component using asyncdata or fetch yet.
Here's the sample response that i see in the browser
{"email":"sample#example.com","account":"123445667777"}
I'm not sure what the issue is?

Related

Connection working from React to NodeJS, but request-url is incorrect in Chrome Network

enter image description hereenter image description hereenter image description here
Assalamu ‘Alaykum
my React app is running on localhost:3000
NodeJS server is running on localhost:1000
I'm sending request to server 'localhost:1000' and everything is fine, correct response is coming from server, there is no error in Console, but in Chrome Network request-url is to localhost:3000/.../...
if I sent wrong data to server, it's coming correct error and bad request error in Console
can someone help me,
thanks
It sounds like your React app is making requests to the wrong URL. Based on the information you provided, it appears that your React app is running on localhost:3000 and your Node.js server is running on localhost:1000, but your requests are being sent to localhost:3000.
To fix this issue, you will need to update your React app to send requests to the correct URL, which should be http://localhost:1000/auth/sign/in. This can typically be done by updating the base URL or endpoint for your API requests in your React app.
For example, if you are using the fetch API to make requests in your React app, you can update the base URL for your requests by setting the baseURL property in your fetch options:
fetch('/auth/sign/in', {
baseURL: 'http://localhost:1000',
// Other fetch options
})
Or, if you are using a library such as Axios to make requests in your React app, you can update the base URL for your requests by setting the baseURL property in the Axios configuration:
import axios from 'axios';
axios.defaults.baseURL = 'http://localhost:1000';
After updating the base URL for your API requests, your React app should send requests to the correct URL, http://localhost:1000/auth/sign/in, and you should no longer see requests being sent to localhost:3000 in the Chrome Network tab.
It's worth noting that the exact solution for updating the base URL for your API requests will depend on the specific details of your React app and how you are making requests to your server. If you are having trouble updating the base URL, you may want to consult the documentation for the specific library or API you are using.

Getting data from my database via my api(express) in react

I am making my first mern app and i want to populate the dashboard when someone logs in i was thinking that I should make a get request in my server and call my database in the get request and do the work from there but I heard somewhere that I shouldn't do this because its dangerous to call sensitive data in get request , so I am not sure that how I should get that data in my react app.
You can use a Post request.
I Recommend the Axios Library it is the most used HTTP Request library
https://www.npmjs.com/package/axios
then do
axios.post("https://url.de/request", {
data1: "hallo",
data2: "secret data"
})
in express the data is then in req.body.data1
EDIT: the Data is only secure when the url starts with https://

Routes with /"model" bypassing routes defined in routes.js

Well, i'm building a back-end project on Sails.js in the default port 1337, and everytime i send a get request like "localhost:1337/user", while user is a model, the server responds with all the content in that table. I have a controller for that model, but this request bypass the entire controller and sends all user informations in the table.
I already tried overwriting the '/user' request with a function that sends nothing in response, but still sending every information in the table.
Anyone can help me with that?

Node.js Express - Get http protocol text for request

In node.js express, if I get a request with an undefined body I would like to log what the http protocol text was so that I can figure out what went wrong. I don't want to flood my logs with every single request made to the server just the one that comes up undefined.

Sending a cookie to web browser using Node.js Express

I am following all the suggestions online about how to send a cookie in an HTTP response in Express.
The simple way would be like so:
app.use(function(req,res,next){
res.cookie('foo', 'bar');
});
however when future requests come to the server from the same web browser, the cookie is not available in req.cookies; in other words, req.cookies['foo'] is undefined.
I have set the front-end code to use "withCredentials" for any AJAX request. Yet the cookie is still not sent to the server, or at least does not appear to be in any way.
One thing that concerns me is that when I call res.cookie('foo','bar') on the server it doesn't show up on the res object/stream as you can see here:
https://www.dropbox.com/s/fe317re9g2frucv/Screenshot%202016-01-21%2023.49.45.png?dl=0
Is there anything obvious that I am doing wrong?

Resources