I'm trying to execute a function in node.js when a users enters information on a form in html. I keep getting 404 not found and am not sure where to go. I'm sure there are other questions similar to this but have searched around and can't find anything.
HTML Code:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<link rel = "stylesheet"
type = "text/css"
href = "style.css" />
<form action="/zipCheck" method="post" accept-charset="utf-8">
<input placeholder="Enter your zip code to start" class = "zipInput">
</form>
</html>
Node.js code:
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
console.log("hello");
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(logger('short'));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.post('/zipCheck', function(req, res){
console.log("GOOD");
});
// 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;
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…