简体   繁体   English

我的项目只显示我的主页,没有任何按钮和其他页面

[英]My project display only my homepage without any buttons and other pages

while making this school project i finish the code and make everything right, at least that's what i thought.在制作这个学校项目时,我完成了代码并使一切正确,至少我是这么想的。

I'll put here my app.js and bin section, so if anyone can help me display everything i would appreciate it.我会把我的 app.js 和 bin 部分放在这里,所以如果有人能帮助我展示所有内容,我将不胜感激。

I start it with npm on the localhost as you can see in the picture.如图所示,我在本地主机上使用 npm 启动它。

I really need help, i am a student and i know the website is not very awesome but that's what they asked us.我真的需要帮助,我是一名学生,我知道该网站不是很好,但这就是他们问我们的。

There is the index.hbs file有 index.hbs 文件

<section> 
    <h1>Welcome to StackPeakFlow.</h1>

    <h3>List of questions</h3>
    <ul>
        {{#each questionTable}}
        <li>{{this}}</li>
        {{/each}}
    </ul>
</section>

There is the index.js router file有 index.js 路由器文件

const router = express.Router();


const messagesTable = [];



/* GET home page. */
router.get('/', (req, res, next) => {
  res.render('index', {
    questionTable: ['Can comments be used in JSON file?', 'How can I center text inside a div block?', 'Does Python have a ternary conditional operator?', 'How do i compare strings in Java?']
  });
});

/* GET question. */
router.get('/question', (req, res, next) => {
  res.render('indexQuestion', { messagesTable });
});

/* POST add question. */
router.post('/question/add', (req, res, next) => {
  console.log("POST ADD QUESTION");
  messagesTable.push({ message: req.body.message, author: req.body.author });
  res.redirect('/question');
});


module.exports = router;
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

// Use of sessions
var session = require('express-session')

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var questionsRouter = require('./routes/questions');
var membersRouter = require('./routes/members');

var app = express();

// register partials views 
// important to do that before set engine 
// BEFORE app.set('view engine', 'hbs');
var hbs = require('hbs');
hbs.registerPartials(__dirname + '/views/partials');

//eq
hbs.registerHelper('eq', function (a, b) {
  if (a === b) {
    return true;
  }
  else {
    return false;
  }
});

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

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(session({ secret: "Your secret key", resave: false, saveUninitialized: false }));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/questions', questionsRouter);
app.use('/members', membersRouter);

// 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;
``` APP.JS







Here is the bin part with the WWW file
```#!/usr/bin/env node

/**
 * Module dependencies.
 */

 var app = require('../app');
 var debug = require('debug')('02-1-exo1-mvc:server');
 var http = require('http');
 
 /**
  * Get port from environment and store in Express.
  */
 
 var port = normalizePort(process.env.PORT || '3000');
 app.set('port', port);
 
 /**
  * Create HTTP server.
  */
 
 var server = http.createServer(app);
 
 /**
  * Listen on provided port, on all network interfaces.
  */
 
 server.listen(port);
 server.on('error', onError);
 server.on('listening', onListening);
 
 /**
  * Normalize a port into a number, string, or false.
  */
 
 function normalizePort(val) {
   var port = parseInt(val, 10);
 
   if (isNaN(port)) {
     // named pipe
     return val;
   }
 
   if (port >= 0) {
     // port number
     return port;
   }
 
   return false;
 }
 
 /**
  * Event listener for HTTP server "error" event.
  */
 
 function onError(error) {
   if (error.syscall !== 'listen') {
     throw error;
   }
 
   var bind = typeof port === 'string'
     ? 'Pipe ' + port
     : 'Port ' + port;
 
   // handle specific listen errors with friendly messages
   switch (error.code) {
     case 'EACCES':
       console.error(bind + ' requires elevated privileges');
       process.exit(1);
       break;
     case 'EADDRINUSE':
       console.error(bind + ' is already in use');
       process.exit(1);
       break;
     default:
       throw error;
   }
 }
 
 /**
  * Event listener for HTTP server "listening" event.
  */
 
 function onListening() {
   var addr = server.address();
   var bind = typeof addr === 'string'
     ? 'pipe ' + addr
     : 'port ' + addr.port;
   debug('Listening on ' + bind);
 }

WWW section万维网部分

And i'll put you a screenshot of what the page looks while started我会给你一张页面启动时的截图

Display site展示网站

and here are the views files views这是视图文件视图

https://handlebarsjs.com/guide/partials.html https://handlebarsjs.com/guide/partials.html

To use the partials, you need to,要使用部分,你需要,

  1. Register them注册他们
  2. Call them给他们打电话

You are registering them properly您正在正确注册它们

But looks like you might be forgetting to call them in your index.hbs ?但是看起来您可能忘记在index.hbs中调用它们?

header at the top, header在顶部,
{{> header}}
and footer at the bottom,和底部的footer
{{> footer}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 GitHub 页面只显示我的 React 应用程序的主页而不显示其他页面? - Why does my GitHub page only show the homepage for my react app and not the other pages? 不良的JavaScript? hello bar solo仅在主页上起作用-没有其他页面 - bad javascript? hello bar solo only working on homepage - no other pages 如何显示我的仅滑块首页? - how to show my slider only homepage? 仅针对主页调用一个函数,然后针对其他任何页面停止该函数,然后再次对其进行调用 - Call a function only for homepage, then stop it for any other page, then call it for homepage again 如何更改按钮而不影响其他按钮? - How to change buttons without having any affect on the other buttons? 在我的主页上使用Dropbox - using Dropbox on my homepage 如何使主题按钮更改器影响网站上的所有页面而不仅仅是主页? - How do i make my theme button changer affect all pages on my site not just the homepage? 我的Javascript Ajax请求可在Phonegap index.html中使用,但不能在其他任何页面中使用,如何解决此问题? - My Javascript Ajax request works in Phonegap index.html but not in any other pages, how can I fix this? 如何使用jquery.cookie仅将cookie设置为主页而不是其他页面 - How to set cookie for homepage only and not other pages using jquery.cookie 仅在 Vue.js 路由器的某些页面上滚动到页面顶部 - 在其他页面上保持滚动位置相同 - Only scroll to top of page on certain pages of my Vue.js router - keep scroll position same on other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM