Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将用户重定向到静态HTML页面时出现问题_Javascript_Node.js_Express_Http_Formidable - Fatal编程技术网

Javascript 将用户重定向到静态HTML页面时出现问题

Javascript 将用户重定向到静态HTML页面时出现问题,javascript,node.js,express,http,formidable,Javascript,Node.js,Express,Http,Formidable,我开始用Javascript和Nodejs编写代码。我正在检查是否已对recFile进行post调用并接受发送的文件。对于所有其他http请求,我希望返回一个静态HTML页面。当我尝试res.redirect时,它说“res.redirect不是一个函数” const bodyparser = require('body-parser'); var http = require('http'); var formidable = require('formidable'); var fs = r

我开始用Javascript和Nodejs编写代码。我正在检查是否已对recFile进行post调用并接受发送的文件。对于所有其他http请求,我希望返回一个静态HTML页面。当我尝试res.redirect时,它说“res.redirect不是一个函数”

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);
试一试


(来自的信息,您是否正确搜索了它?

res.redirect()
是Node.js库“Express.js”中的函数,不是本机方法。这是否回答了您的问题?看起来response.writeHead会发送用户从浏览器请求该URL。但我需要直接在浏览器上呈现该页面作为输出。我在尝试别的东西。可能是我的问题太基本了。我发布了一个不同的问题,因为我正在尝试一种不同的方式将我的文件放入节点js
response.writeHead(302, {
  'Location': 'your/404/path.html'
  //add other headers here...
});
response.end();