Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
Express post/send message to route

I have the route /test:

app.route('/test', (req,res)=>{
    res.sendFile(__dirname + "\\myhtml.html")
})

And I need to have node fire an event on my /test route, and the myhtml.html file can listen for that event, retrieve the data sent through when the server fires it. My example (not real) code is:

page = app.route('/test', (req,res)=>{
    res.sendFile(__dirname + "\\myhtml.html")
})

page.fire('mything',{one:2})

And then in myhtml.html,

idk_express_parent_thing.on('mything', (res)=>{console.log(res)})

Without socket io, should be native express, js, idk.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I'm not sure if this may be able to help you, but I'm going to try.

I thought about three possible solutions...:

  1. Maybe, socketing may be a solution in your case, using Socket.IO, or something. In this case, the back-end would send the file to the client, which when rendered would connect to the server and listen for something, maybe using some id. I just think that maybe it would bring too much complexity, but... it's maybe a solution...

  2. Another one, probably more suitable, can be that you store the message that you need to be sent, somewhere (I recommend Redis, or you can try using it in server memory) and then, after the server sends the file to the client, the client makes a request to the server, asking for that message by sending the Redis key used to identify the stored message

  3. A last one, is maybe using cookies, session and related... Where, you would simply save the message into cookies/session, along with the file, then, the file would have access to the data when on the client

I also did some research, and I found this StackOverflow anwnser (the second answer is going to maybe help you, the first one talks about EJS and other engines... which you said you can't use)

Hope it helps! ;D

about 4 years ago · Juan Pablo Isaza Report

0

you have to set route correctly. if you have myhtml.html file in root direcotry app.route('/test', (req,res)=>{ res.sendFile(__dirname + "/myhtml.html") })

it will be work

about 4 years ago · Juan Pablo Isaza Report

0

So I am answering my own question because I found out about js WebSockets, and the WebSocket-Node lib - they allow full-duplex communication beetween server, and client, and are simple to use. here is server code:

const nwss = require('websocket').server;
const http = require("http");

var server = http.createServer(function(request, response) {
    console.log(' Received request for ' + request.url);
    response.write("example")
    response.end();
});

server.listen(8000, ()=>{console.log("ready")})

ws = new nwss({
    httpServer: server,
    autoAcceptConnections: false
})
ws.on('request', (req)=>{
    let conn = req.accept('example-protocol', req.origin);
    console.log('accepted ' + conn.remoteAddress)
    conn.on('message', (msg)=>{
        if (msg.type == 'utf8') {
            console.log('Recieved utf8 ' + msg.utf8Data);
            conn.sendUTF(`thanks for "${msg.utf8Data}"`)
        }
    })
    conn.on('close', ()=>{
        console.log(`${conn.remoteAddress} disconnected.`)
    })
})

(it is a bad idea to allow other sites to connect, so use an if statement to check if the origin is ws://yoursiteurl/) And client:

let socket_url = "ws://" + new URL(location.href).host + "/"
let WSocket = window['MozWebSocket'] ? MozWebSocket : WebSocket;
let socket_ = new WSocket(socket_url, 'example-protocol') //the example-protocol is the same i put in the server code, as it has to be.
function start() {
    socket_.onmessage = (msg)=>{
        console.log(msg.data)
    }
    socket_.send("hi, do you read? :P")
}
//call start once it connects.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!