'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.
I start it with npm on the localhost as you can see in the picture.
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
<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
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
and here are the views files views
Solution 1:[1]
https://handlebarsjs.com/guide/partials.html
To use the partials, you need to,
- Register them
- Call them
You are registering them properly ?
But looks like you might be forgetting to call them in your index.hbs
?
header
at the top,
{{> header}}
and footer
at the bottom,
{{> footer}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | shramee |