当我在网站上提交文件上传表单时,为什么会收到以下404 NotFoundError?我认为该错误是由于未安装urlencodedParser中间件(在回溯的最后一行),但是即使在我将中间件安装并添加到app.js之后,我仍然遇到相同的错误。
NotFoundError: Not Found
at /Users/katiegu/jobfinderapp/Resume-to-Job/app.js:54:8
at Layer.handle [as handle_request] (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:317:13)
at /Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:335:12)
at next (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:275:10)
at /Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:635:15
at next (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:260:14)
at Function.handle (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:174:3)
at router (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:317:13)
at /Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:335:12)
at next (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/express/lib/router/index.js:275:10)
at urlencodedParser (/Users/katiegu/jobfinderapp/Resume-to-Job/node_modules/urlencoded-parser/dist/index.js:56:3)
这些是我的路线(uploadresume / post出现此错误。):
var express = require('express');
var router = express.Router();
var home_controller = require('../controllers/homeController');
/* GET home page. */
router.get('/', home_controller.index);
router.get('/login', home_controller.login);
router.post('/login', home_controller.login_post);
router.get('/signup', home_controller.signup);
router.post('/signup', home_controller.signup_post);
router.get('/uploadresume', home_controller.upload_resume);
router.get('/uploadresume/post', home_controller.upload_resume_post);
module.exports = router;
这些是我的控制器。 upload_resume_post控制器从HTML表单获取文件输入,并将其发送到Google Vision API服务器。
exports.upload_resume_post = function(req, res, next) {
const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient();
var reader = new FileReader();
var file = document.getElementById('file').files;
var b64url = reader.readAsDataURL(file[0]);
client.documentTextDetection(b64url)
.then(results => {
const fullTextAnnotation = results[0].fullTextAnnotation;
console.log(fullTextAnnotation.text);
})
.catch(err => {
console.error('ERROR:', err);
});
};
这是我要上传文件的表单的视图:
doctype html
html(lang='en')
head
meta(charset='utf-8')
meta(name='viewport', content='width=device-width, initial-scale=1')
link(rel='stylesheet', href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css')
script(src='https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js')
script(src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js')
link(rel='stylesheet', href='/stylesheets/upload_resume.css')
body
h1 Upload Resume
form(method='post', action='/uploadresume/post', enctype='multipart/form-data')
label(for='file') Choose File to Upload.
br
br
input(type='file', id='file', accept='image/*')
br
br
input(type='submit', id='submit', value='Submit')
这是我的app.js。我导入和添加bodyParser和urlencodedParser中间件的方式是否正确?
var express = require('express');
var app = express();
var path = require('path');
var createError = require('http-errors');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');
var urlencodedParser = require('urlencoded-parser');
var mongoose = require('mongoose');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var mongodb = 'mongodb:/katie_gu:database4project@ds163850.mlab.com:63850/jobfinderapp';
mongoose.connect(mongodb);
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'work hard',
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: db
})
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(urlencodedParser);
var indexRouter = require('./routes/index');
var catalogRouter = require('./routes/catalog');
var usersRouter = require('./routes/users');
var profileRouter = require('./routes/profile');
app.use('/', indexRouter);
app.use('/catalog', catalogRouter);
app.use('/users', usersRouter);
app.use('/profile', profileRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;