So my app gets data from my database but it send a response with " \ "
Express response
[
{
"ID": "account-saassan",
"data": "{\"uuid\":\"saassan\",\"username\":\"sasasasasa\"}"
}
]
Console output:
[
{
ID: 'account-saassan',
data: '{"uid":"saassan","username":"sasasasasa"}'
}
]
Code:
const UserData = new db.table("userdata")
app.use((req, res, next) => {
res.append('Access-Control-Allow-Origin', ['*']);
res.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.append('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.get("/list", (request, response) => {
let data = UserData.all()
console.log(data)
response.type('json');
response.json(data)
// I've tried with response.send too. And without response.type
})
app.listen(8080)
Why does it add "" in the response and how do I fix it?
Try to using JSON.stringify
response.json(JSON.stringify(data))
Add please add this :
response.setHeader('Content-Type', 'application/json');
Related
I'm having some troubles trying to make my backend and front communicate to each others (reactjs + vanilla NODEjs), both on different localhost port (4000 and 3000)
I managed to make it work until i wanted to try to add data to my database.
I can GET data with my react app but i can't POST any data.
Here is the code for both sides :
front :
`
const sendData = async (event: any): Promise<void> => {
console.log("hello");
event.preventDefault();
const response = await fetch(`${_DATA_URL_}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(employeeForm),
});
const data = await response.json();
console.log(data);
};
`
back :
const Router = (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Content-Type, Accept, Origin, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, PATCH, OPTIONS"
);
console.log("hello"); //debug
if (req.method === "GET") {
if (req.url === "/api/employees") {
getEmployees(req, res);
} else if (req.url.match(/\/api\/employees\/(\w{4}-?){3}/)) {
const requestID = req.url.split("/")[3];
getEmployee(req, res, requestID);
} else {
res.statusCode = 404;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ message: `Error : ${req.url} not found !` }));
}
}
if (req.method === "POST") {
if (req.url === "/api/employees") {
createEmployee(req, res);
} else {
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ message: `Error : ${req.url} not found !` }));
}
}
if (req.method === "DELETE") {
if (req.url.match(/\/api\/employees\/(\w{4}-?){3}/)) {
const requestID = req.url.split("/")[3];
deleteEmployee(req, res, requestID);
} else {
res.statusCode = 404;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ message: `Error : ${req.url} not found !` }));
}
}
if (req.method === "PUT") {
if (req.url.match(/\/api\/employees\/(\w{4}-?){3}/)) {
const requestID = req.url.split("/")[3];
updateEmployee(req, res, requestID);
} else {
res.statusCode = 404;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ message: `Error : ${req.url} not found !` }));
}
}
};
i'm pretty sure it comes from react side because posting data works fine when i use POSTMAN, and after a while on localhost i receive this error in the console :
Maybe i'm using cors incorrectly, anyways thanks for your futures answers
I figured it out,
answer here (in french) and here
The frontend is index.html:
axios.post('http://localhost:1337/', {
clientId: document.getElementById('clientId').value,
cmd: document.getElementById('cmd').value,
type: document.getElementById('type').value,
amt: document.getElementById('amt').value,
merchantRef: document.getElementById('merchantRef').value,
channel: document.getElementById('channel').value,
successUrl: document.getElementById('successUrl').value,
failureUrl: document.getElementById('failureUrl').value,
webhookUrl: document.getElementById('webhookUrl').value,
goodsName: document.getElementById('goodsName').value
})
.then(function (response) {
var json = response.data;
document.getElementById('sessionId').innerHTML = json['sessionId'];
SpiralPG.init(json['sessionId']);
})
.catch(function (error) {
console.log(error);
});
The backend is server.js:
http.createServer(async function (req, res) {
try {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST');
res.setHeader('Access-Control-Allow-Headers', '*');
const buffers = [];
for await (const chunk of req) {
buffers.push(chunk);
};
const data = Buffer.concat(buffers).toString();
saleSession(res, data);
}
catch (error) {
console.log(error);
}
}).listen(port);
something inside function saleSession(); in the server.js to get the data from frontend(if I hard code in here, there will be no error, but change to input data from frontend, there will be error):
var merchantRef = JSON.parse(data).merchantRef;
let bodyData =
{
'clientId': JSON.parse(data).clientId,
'cmd': JSON.parse(data).cmd,
'type': JSON.parse(data).type,
'amt': JSON.parse(data).amt,
'merchantRef': merchantRef,
'channel': JSON.parse(data).channel,
'successUrl': JSON.parse(data).successUrl,
'failureUrl': JSON.parse(data).failureUrl,
'webhookUrl': JSON.parse(data).webhookUrl,
'goodsName': JSON.parse(data).goodsName
}
I'm just copy the code in https://nodejs.dev/learn/get-http-request-body-data-using-nodejs, which is node.js office website. Why the output is:
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at saleSession (C:\Users\JoeChan\Desktop\my-nodejs2\server.js:41:28)
at Server.<anonymous> (C:\Users\JoeChan\Desktop\my-nodejs2\server.js:104:9)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
So I am trying to access the body of a request I am sending. I don't want to incorporate Express for this project. I understand that I essentially have to stream the body in from the incoming request, however it seems that my approach isn't pulling any data from the request. Here is what I have
The request
var query = document.getElementById('search').value;
console.log(query);
axios({
method: 'get',
url: url,
headers: {},
data: {
query: query
}
}).then((response) => {
if(response.status == 200){
console.log(response);
this.tweet = response;
}
}).catch((error) => {
console.log(error);
});
Where I am handling the request
http.createServer((req, res ) => {
//Setting CORS Headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
if ( req.method === 'OPTIONS' ) {
res.writeHead(200);
res.end();
return;
}
else if(req.url == "/search"){
let body = 'Our Data: ';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
console.log(body);
});
//console.log(data);
callTweetAPI.callTweetAPI(function(response) {
res.write(JSON.stringify(response));
res.end();
});
}
}).listen(port);
The output I expect would be the data I sent in the axios request so 'query', but I end up with blank output. It seems that the request being sent through doesn't have a data block available?
So what I'm trying to do is to manualy overwrite response when there is a request to my json-server. I'm fine with that, but I dont know how to add a bit to also store the original request in database.
Here is my code (i'm sending as response the name from request and static uuid)
The next(); in if step is failing complaining it cannot setup headers.
module.exports = (req, res, next) => {
if (req.path == "/business") {
res.status(201);
res.jsonp({
id: "a23e1b13-cf69-461c-aa8a-a0eb99e41350",
name: req.body['name'],
revision: "1"
});
next();
}
else {
next();
}
}
If you want to pass data from middleware to any routes you can assign value with req object rather than res object.
app.use(function(req.res)) {
req.custom_data = {
id: "a23e1b13-cf69-461c-aa8a-a0eb99e41350",
name: req.body['name'],
revision: "1"
};
next();
}
From other location you can do like this.
module.exports = (req, res, next) => {
res.jsonp(req.custom_data)//something like this
}
It might help you.
You are right.
For example, we cannot:
server.js
const jsonServer = require("json-server")
const server = jsonServer.create()
const router = jsonServer.router("db.json")
server.use(jsonServer.defaults())
server.use(jsonServer.bodyParser)
server.use((req, res, next) => {
if (req.method === "POST" && req.url === "/business") {
res.status(201)
res.jsonp({
id: "a23e1b13-cf69-461c-aa8a-a0eb99e41350",
name: req.body["name"],
revision: "1"
})
}
next()
})
server.use(router)
server.listen(3000, () => {
console.log("JSON Server is running")
})
Or we get: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client when we run node server.js, because we have already set and sent the headers when we call res.jsonp, so we cannot do this again by calling server.use(router).
We have a couple options.
Option 1.
server.js
const jsonServer = require("json-server")
const server = jsonServer.create()
const router = jsonServer.router("db.json")
server.use(jsonServer.defaults())
server.use(router)
server.listen(3000, () => {
console.log("JSON Server is running")
})
db.json
{
"business": []
}
Now we can POST to our http://localhost:3000/business endpoint and save our request body to our database. However, we cannot amend the 201 status, nor the request body to something else.
Option 2.
server.js
const jsonServer = require("json-server")
const server = jsonServer.create()
server.use(jsonServer.defaults())
server.use(jsonServer.bodyParser)
server.post("/business", (req, res, next) => {
res.status(201)
res.jsonp({
id: "a23e1b13-cf69-461c-aa8a-a0eb99e41350",
name: req.body["name"],
revision: "1"
})
next()
})
server.listen(3000, () => {
console.log("JSON Server is running")
})
Now we get the correct response, but we can no longer save to our database business key, because we are calling server.post("/business"...
You could however use this in your own code and save it somewhere else (or write to your own database), as this response will be returned from a Promise.
im trying to get another api using node request, but when i try to display body.data its return undefined
router.get('/', (req, res,next) => {
request('http://localhost:3000/api/v1/promos', (error, response, body) => {
if(error) {
res.send('An erorr occured')
console.log(error)
}
else {
res.send(body.data)
}
});
});
anyone know whats wrong
If the body of your request is JSON, then you have to parse it to turn it into a Javascript object so you can access the .data property.
You can do that by passing the json: true option to request() and it will parse the JSON for you.
Or, you can manually parse the body with let result = JSON.parse(body).
Here's how you set the json: true option:
router.get('/', (req, res,next) => {
request({
url: 'http://localhost:3000/api/v1/promos',
json: true
}, (error, response, body) => {
if(error) {
res.send('An erorr occured')
console.log(error)
}
else {
res.send(body.data)
}
});
});