Hi, everyone.
I cant use the handler endpoint in JS.
Someone can make an example ?
index.html
const click = ref(() => {
fetch('http://localhost:30120/testnode/ping') // 30120 é a porta padrão do servidor FiveM
.then(response => {
if (!response.ok) {
throw new Error('Erro ao fazer a solicitação HTTP');
}
return response.text();
})
.then(data => {
console.log(data); // Saída: 'Hello from my custom route!'
})
.catch(error => {
console.error('Erro:', error);
});
})
server.js
setHttpHandler(myHttpHandler);
// Defina o manipulador de rota HTTP personalizado
function myHttpHandler(request, response) {
if (request.method === 'GET' && request.path === '/ping') {
// Se uma solicitação GET foi feita para o caminho `/ping`
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.send('pong');
} else {
// Caso contrário, retorne uma resposta 404 Not Found
response.writeHead(404);
response.send();
}
}
Could you explain a bit better what you are trying to achieve? Because what I see doesn’t seem right, but I don’t know what you’re trying to do to help you.
sorry, i dont speak english fluently.
I want to send blob or readablestream and anothers data from server.js to browser-side
I want to create my own route in the server, and i want to can connect from browser-side
If you want your resource to be accessible from a browser/internet, you have to create an HTTP server on the server. I think it’s the only way to do it.
server.js
// server.js
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
if (req.url === '/my-own-route') {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('¡Hi from FiveM!');
} else {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Route no found');
}
});
const port = 8080;
server.listen(port, () => {
console.log(`Web Server start at port ${port}`);
});
Now, from the browser, you can access and send the information you want to the server http://localhost:8080/my-own-route .
From the client side in index.html, you shouldn’t use localhost; instead, you should use the server’s IP.
The server will work locally, but if you use localhost in production, it won’t.
If this is what you are looking for, be careful as you may encounter some security issues.