快递路线无法到达

时间:2018-06-07 08:50:59

标签: node.js express

我有一个admin和主页的路由文件。我也有家庭和管理员的不同布局文件。当我访问本地开发人员的主页和管理员路线时,一切正常,但当我尝试访问使用site.com/admin的管理路由时,我得到了无法获取/管理/响应。以下是我的server.js:

const express = require('express');
const exphbs = require('express-handlebars');
const methodOverride = require('method-override');
const flash = require('connect-flash');
const session = require('express-session');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const path = require('path');


const app = express();


// Use path
app.use(express.static(path.join(__dirname, 'public')));

// Map Global Promise
mongoose.Promise = global.Promise;

// Connect to mongoose
mongoose.connect('mongodb://localhost/skillbuild')
    .then(() => console.log('MongoDB Connected...'))
    .catch(err => console.log(err));

// Load Idea model
// require('./models/Idea');
// const Idea = mongoose.model('ideas');

// Express Handlebars middleware
app.engine('handlebars', exphbs({
    defaultLayout: 'main'
}));
app.set('view engine', 'handlebars');

// BodyParser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Method Override middleware
app.use(methodOverride('_method'));

// Load routes
const home = require('./routes/home');
const admin = require('./routes/admin');


// Use routes
app.use('/', home);
app.use('/admin', admin);

// Express session middleware
app.use(session({
    secret: 'keyboard cat',
    resave: true,
    saveUninitialized: true
}));

app.use(flash());

// Global variables
app.use(function (req, res, next) {
    res.locals.success_msg = req.flash('success_msg');
    res.locals.error_msg = req.flash('error_msg');
    res.locals.error = req.flash('error');
    next();
});




const port = 80;

app.listen(port, () => {
    console.log(`Server started on port ${port}`);
});

我的家庭路线在home.js路线文件中:

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

router.all('/*', (req, res, next) => {
    req.app.locals.layout = 'main';
    next();
});

// index route 
router.get('/', (req, res) => {
    res.render('index');
});


// About route 
router.get('/about', (req, res) => {
    res.render('about');
});


//login route temp 
router.get('/login', (req, res) => {
    res.render('users/login');
});

module.exports = router;

我的admin.js路由文件:

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

router.all('/*', (req, res, next) => {
    req.app.locals.layout = 'admin';
    next();
});

// admin index route 
router.get('/', (req, res) => {
    res.render('admin/index');
});

module.exports = router;

1 个答案:

答案 0 :(得分:0)

好的......所以问题只是我需要使用节点应用程序的pm2 restart id重新启动服务器才能加载更新的代码。问题解决了。