0

I am beginning to code in Javascript and Nodejs. I am checking if a post call has been made to recFile and accept the file sent. For all other http requests made I want to return a static HTML page. When I tried res.redirect it said 'res.redirect is not a function'

const bodyparser = require('body-parser');
var http = require('http');
var formidable = require('formidable');
var fs = require('fs');
const express =require('express');
const { response } = require('express');
const app = express();


http.createServer(function (req, res) {
  if (req.url == '/recFile') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      var oldpath = files.filetoupload.path;
      var newpath = './Uploads/' + files.filetoupload.name;
      fs.rename(oldpath, newpath, function (err) {
        if (err) throw err;
        res.write('File uploaded and moved!');
        res.end();
      })
      });

    } else {

// Need to send user here to a HTML page. But don't know how to do that. 
// res.write is not viable as there is a lot of HTML lines of code.

        return res.end();
      }
}).listen(9000);

1 Answers1

1

Try

response.writeHead(302, {
  'Location': 'your/404/path.html'
  //add other headers here...
});
response.end();

(Information from Nodejs - Redirect url, did you google it properly?)

Temoncher
  • 644
  • 5
  • 15
  • It looks like the response.writeHead sends the user to make a request for that URL from the browser. But I need to render that page directly on the browser as an output. I am trying something else. May be my question is too basic. I am posting a different question as I am trying a different way to get my files into node js – ThisAndThat Aug 09 '20 at 18:41