将 /upload 网页(从 node.js 中的表单发布请求获得)重定向到另一个 html 页面

我是 node.js 的新手。我无法将页面 /upload 重定向到另一个 .html 网页。我的意思是,一切正常,我上传了 img 文件,代码转到http://localhost:3000/upload页面,但我不知道如何自动重定向到另一个页面。.html 文件


这是我的 HTML:


<div class="container">

        <form action="/upload" method="POST" enctype="multipart/form-data">

            <input name="myImage" type="file">

            <input class="file-path validate" type="text" placeholder="Upload your file">

    </div>

    <button type="submit" class="btn">Submit</button>

    </form>

    </div>

这里是我的服务器文件 index.js


const express = require("express");

const multer = require("multer");

const path = require("path");

const storage = multer.diskStorage({

    destination: './public/uploads',

    filename: function(req, file, cb) {

        cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));

    }

});

//init upload

const upload = multer({

    storage: storage,

    limits: { filesize: 1000000 },

    fileFilter: function(req, file, cb) {

        checkFileType(file, cb);

    }


}).single('myImage'); //single image


function checkFileType(file, cb) {

    const filetypes = /jpeg|jpg|png|gif/;

    const extname = filetypes.test(path.extname(file.originalname).toLowerCase());

    const mimetype = filetypes.test(file.mimetype);

    if (mimetype && extname) {

        return cb(null, true);

    } else {

        cb("Error:images only");

    }

};

const app = express();

app.use(express.static('./public'));

app.post('/upload', (req, res) => {

    upload(req, res, (err) => {

        if (err) {

            res.json(err);            

        } else {

            if (req.file == undefined) {

                res.json("Error: No file selected")                  

            } else {

                res.json(`/${req.file.filename}`);

            }

        }

    });

});


app.listen(3000, function(req, res) {

    console.log("you are listening to port 3000");

});

我登陆http://localhost:3000/upload并留在这里。如何跳过它并将此页面自动重定向到另一个代码 html 页面?谢谢你的帮助


catspeake
浏览 137回答 1
1回答

肥皂起泡泡

您只需要使用res.redirect('/path/here/);您的路线下的功能。完整设置://Dependenciesconst multer = require('multer');//Multer DiskStorage Configconst diskStorage = multer.diskStorage({&nbsp; &nbsp; destination: 'assets/profile_upload',&nbsp; &nbsp; filename: (req, file, call_back) => {&nbsp; &nbsp; &nbsp; &nbsp; //Prepend date to the filename or anything that makes&nbsp; &nbsp; &nbsp; &nbsp; //the file unique so it won't be overwritten&nbsp; &nbsp; &nbsp; &nbsp; call_back(null, Date.now() + '_' + file.originalname);&nbsp; &nbsp; }});//Create Multer Instanceconst upload = multer({ storage: diskStorage });&nbsp;//Picture upload//or app.post()router.post('/upload-pic', upload.single('file'), (req, res) => {//The redirection happens here.console.log(req.file);res.redirect('/your/path');});//Your code:app.post('/upload', (req, res) => { ...try doing app.post('/upload' ,upload.single('file'), (req,res) =>{ ...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript