How do I set up a unique road handler?
githubpeti85 opened this issue · comments
I want to set it up as a post request, but it always gives me a 404 error.
Server js file:
const jsonServer = require("json-server");
const cors = require("cors");
const path = require("path");
const fs = require('fs');
const server1 = jsonServer.create();
const middlewares1 = jsonServer.defaults();
server1.use(cors());
server1.use(jsonServer.bodyParser);
server1.use(middlewares1);
server1.post('/archiv', (req, res) => { // I would like to send it to the archive path and then write the data to a file.
fs.writeFile('output.json', req.body, (err) => {
if (err) {
console.log(err);
res.status(500).send('error in write.');
} else {
res.status(200).send('writed file.');
}
});
})
const router1 = jsonServer.router(path.join(__dirname, "db1.json")); //The standard handler is then called correctly.
server1.use(router1);
server1.listen(3000, () => {
console.log("Server is running at http://localhost:3000");
})
Client js file:
function postServerData3000(route, sending) {
let fetchOp = {
method: 'POST',
cache: 'no-cache',
mode: 'cors',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(sending) // The shipment is already stringed here
};
return fetch(`http://localhost:3000/${route}`, fetchOp) // The full url is specified and the archive string is substituted
.then(response => response.json(), err => console.error(err));
}
@githubpeti85 Your setup seems almost correct, but there are a couple of adjustments needed to ensure your POST request works as intended. Here are the modifications:
Use JSON Server's bodyParser middleware properly: Instead of jsonServer.bodyParser, you should require json-server/lib/server and use its bodyParser function.
Check if the request is properly routed: Ensure that your client is sending data to the correct endpoint (/archiv).
Properly handle incoming JSON data in the server: When writing the data to a file, you need to convert it back from string to JSON format.
Here's the modified server.js file:
const jsonServer = require("json-server");
const cors = require("cors");
const path = require("path");
const fs = require('fs');
const server1 = jsonServer.create();
const middlewares1 = jsonServer.defaults();
server1.use(cors());
server1.use(jsonServer.bodyParser); // Importing bodyParser from json-server package
server1.use(middlewares1);
server1.post('/archiv', (req, res) => {
fs.writeFile('output.json', JSON.stringify(req.body), (err) => { // Convert req.body to JSON string
if (err) {
console.log(err);
res.status(500).send('error in write.');
} else {
res.status(200).send('writed file.');
}
});
});
const router1 = jsonServer.router(path.join(__dirname, "db1.json"));
server1.use(router1);
server1.listen(3000, () => {
console.log("Server is running at http://localhost:3000");
});
And the client.js file should remain the same.
With these modifications, your POST request should work properly. Make sure your client is sending the data to http://localhost:3000/archiv. Also, ensure that sending is a valid JSON object before stringifying it in your client code.