I
I
IB4952021-05-21 17:28:21
Node.js
IB495, 2021-05-21 17:28:21

How to make a redirect to an html page if the statistics are kept on a different port?

I have a server that gives statics and a server that processes data submitted by the user. When I process the data, I want to redirect the user to the main html page, but when I do that, the redirect does not occur, but I get the url of the page I am redirecting to. Could you suggest something to me?
statics server

http.createServer(async (req, res) => {
    const url = '/' === req.url  ? 'index' : req.url.slice(1);
    const fileExt = path.extname(url);
    const fileName = !fileExt ? url.concat('.html') : url; 
    try {
      console.log(url);
      res.setHeader('Access-Control-Allow-Origin', '*');
      res.setHeader('Access-Control-Allow-Headers', 'origin, location, accept, content-type');
      if (req.method === 'OPTIONS') {
        return res.end();
      } else {
        fs.createReadStream(path.join(__dirname, 'static', fileName)).pipe(res);
      }
    } catch(e) {
      console.log(e);
      res.statusMessage = 404;
      res.end('<h1>File not found</h1>');
    }
  }).listen(port, console.log(`Static on port ${port}`));

server that handles post requests
http.createServer(async (req, res) => {
  const url = req.url;
  const args = await receiveArgs(req);
  if (args) {
    route[url](args);
    res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:8000');
    res.writeHead(302, {'Location': 'http://127.0.0.1:8000/'})
    return res.end();
  }
  res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:8000');
  res.setHeader('Access-Control-Allow-Methods', 'POST');
  res.setHeader('Access-Control-Allow-Headers', 'origin, content-type, accept, Location');
  res.end();
}).listen(PORT, '127.0.0.1', console.log(`Server on port ${PORT}`));

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question