错误404未找到错误

时间:2018-04-28 05:29:37

标签: node.js express

在我的index.ejs中,有一个链接指向/ signup:

<a href="/signup"> Sign Up Here </a>

这是我的app.js:

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();
//connect to mysql
var mysql = require('mysql');
var bodyParser = require('body-parser');
var connection = mysql.createConnection({
  host:'localhost',
  user: 'root',
  password: '',
  database: 'test'
});

connection.connect();
global.db = connection;

// all environments
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

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('/', indexRouter);
app.use('/users', usersRouter);

// 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');
});

app.get('/',indexRouter); //call to index site
app.get('/login', indexRouter); // call to login site
app.get('/signup', usersRouter);

module.exports = app;

这是我的users.js:

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
  message = '';
   if(req.method == "POST"){
      //post data

   } else {
      res.render('signup.ejs');
   }
}); 

module.exports = router;

在我的索引页面中有一个链接链接,用于将用户引导至注册页面。当我点击应该指向“http://localhost:3000/signup”的链接时,它会显示错误404未找到错误。我是NodeJs的新手。任何帮助深表感谢。谢谢。

1 个答案:

答案 0 :(得分:1)

将index.ejs中的链接更改为

// add support-fragment removed from Glide.
implementation "com.android.support:support-fragment:26.1.0"

implementation ('com.github.bumptech.glide:glide:4.1.0') {
   exclude group: 'com.android.support'
   exclude module: 'support-fragment'
   exclude module: 'appcompat-v7'
}

将此处理程序添加到users.js

<a href="/users/signup"> Sign Up Here </a>