Home > other >  Routes in Express.js and jade/pug not routing and consistently crashing with errors
Routes in Express.js and jade/pug not routing and consistently crashing with errors

Time:06-19

Below is my contact router:

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

/* GET users listing. */
router.get('/', function(req, res, next) {
    res.render('contact');
});

module.exports = router;

My app.js and server.js is as follows:

// app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const port = 5000;

//
// ----------------------------------------------------------------
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
// -------------------------------
var contactRouter = require('./routes/contact');
var mealRouter = require('./routes/meal');
var gadginRouter = require('./routes/gadgin');
var aboutRouter = require('./routes/about');
// ----------------------------------------------------------------
var app = express();
//app.listen(port, () => {
//console.log(`Server started at port ${port}`);
//});
// view engine setup
//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');

// ----------------------------------------------------------------
app.set('views', './views');
app.set('view engine', 'jade');

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

app.get('/member/:name/planet/:home', (req, res) => {
    const memberDetails = {
        member: req.params.name,
        planet: req.params.home
    }
    res.render('guardian', memberDetails);
});

app.get('/contact', (req, res) => {

    res.render('contact');
});

app.get('*', (req, res, next) => {
    res.status(200).send('Sorry, page not found');
    next();
});

app.listen(port, () => {
    console.log(`Server started at port ${port}`);
});
// ----------------------------------------------------------------
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);

//----------------------------------------------------------------
app.use('/about', aboutRouter);
app.use('/contact', contactRouter);
app.use('/gadgin', gadginRouter);
app.use('/meal', mealRouter);

// 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('/', (req, res, next) => {
//    res.render('index');
//});
//
module.exports = app;

// index.js
var express = require('express');
var router = express.Router();
var app = express();

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index');
});

module.exports = router;

I want to be able to route to '/' into the index.jade page and '/contact' into the contact.pug page. I keep receiving an error related to title for some reason. I am new to route. I got one route working which is the guardian route.

doctype html
html
  head
    title Express.js   Pug Demo
  body
    h1 Welcome, Guardian!
    div Member: #{member}
    p Planet: #{planet}

My index.jade and contact.jade is as bellow:

extends layout

block content
  h1= WELCOME!
  p Welcome to X, we are trying to figure out the title. HAPPY ERRORS!

  p Hello world!


  li About Miss.Rican 
  li Meal Plan Generator 
  li The GadGin 
  li Contact Information
  li Gadfit Data Base 
  li Contribute to the project

  p Gadfit is the ultimate fitness engine and application. With tailored 
  p algorithms, work out programs, custimization and meal plans you or your trainer can 
  p create, modify and learn more about fitness in a way never before seen. 
  p This is a companion fitness program and is meant to assist and direct clients. 
  p Trainers can use Gadfit to explain their programs, make new fitness plans, custimize meals plans and set up meetings. 

  //a(href=url) 

I Keep getting routed to my error.js page and IDK why it keep referring to a title when I deleted the ${title} tag in the jade files and other respective files. I am new to routing and I want to master it but I am stuck. I promise to contribute to the community once I master the art of routing. Routing is essential and sexy.

//error.jade
extends layout

block content
  h1= message
  h2= error.status
  pre #{error.stack}

CodePudding user response:

extends layout 

block content
    h1= 'Contact Information'

    li Name: Noah Cameron Q'yain 
    li City: New York City 
    li State: New York State 
    li Country: New York State
    li Phone Number: 862-226-6380
    li Email: [email protected] 
    li Fax: [email protected]
    li Github: [email protected]

I GOT IT! In the /views/layout.pug there was a reference to the title and that is extended unto the other views and causes an error, I changed it to equal hello world!

doctype html
html
  head
    title = 'hello world'
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    block content

https://www.youtube.com/watch?v=pCAzGDrtvUY

  • Related