I have been trying to test out express and express handlebars. I have read and tried a few demos. They all basically the same but I cannot get any of them to work. The error I keep getting is -
app.engine('handlebars', exphbs());
^
TypeError: exphbs is not a function
here is my code below:
const express = require("express");
const exphbs = require("express-handlebars");
const app = express();
const port = 8000;
//Handelbars Middleware
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");
// Index Route
app.get("/", function (req, res) {
res.render("home");
});
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
I made a short video to further explain - https://www.awesomescreenshot.com/video/6352907?key=df18cdbdf4ed12b85d2c92458ad9a2de
I thought const exphbs = require('express-handlebars'); was declared a function
Thanks
exphbs is not function u can try this code..
const express = require('express');
const exphbs = require('express-handlebars');
// const { engine } = require('express-handlebars');
const app = express();
const port = 8000;
//Handelbars Middleware
//app.engine('handlebars', engine());
app.engine('handlebars', exphbs.engine());
app.set('view engine', 'handlebars');
// Index Route
app.get('/', function (req, res) {
res.render('home');
});
app.listen(port, () =>{
console.log(`Server started on port ${port}`);
});
I think You should use example as reference from express-handlebars. To use this code you need add to your pacakge.json file this line "type": "module",(ES6 import) Good Luck!
import express from 'express';
import { engine } from 'express-handlebars';
const app = express();
app.engine('handlebars', engine());
app.set('view engine', 'handlebars');
app.set('views', './views');
app.get('/', (req, res) => {
res.render('home');
});
app.listen(8000);
Related
I keep getting "app.engine('handlebars', handlebars()); type error 'handlebars' is not a function? Is there anything wrong in my code?
const express = require('express');
const nodemailer = require("nodemailer");
const paths = require('path');
const handlebars = require('express-handlebars')
const app = express();
require("dotenv").config();
//View engine setup
app.set("view engine", 'handlebars');
app.engine('handlebars', handlebars());
//Static Folder
app.use('/public', express.static(path.join(__dirname, 'public')))
//Body Parser MiddleWars;
app.use(express.urlencoded({extended: false}));
app.use(express.json())
const port = 3001;
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
According to the examples express-handlebars provided, you must import the engine function from the module:
(Using Imports)
import express from 'express';
import { engine } from 'express-handlebars';
const app = express();
app.engine('handlebars', engine());
app.set('view engine', 'handlebars');
app.set("views", "./views");
app.get('/', (req, res) => {
res.render('home');
});
app.listen(3000);
So, instead of calling handlebars directly, you should be calling handlebars.engine.
In total:
const express = require('express');
const nodemailer = require("nodemailer");
const paths = require('path');
const handlebars = require('express-handlebars')
const app = express();
require("dotenv").config();
//View engine setup
app.set("view engine", 'handlebars');
app.engine('handlebars', handlebars.engine());
//Static Folder
app.use('/public', express.static(path.join(__dirname, 'public')))
//Body Parser MiddleWars;
app.use(express.urlencoded({extended: false}));
app.use(express.json())
const port = 3001;
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
I am a newbie of node.js, i use template engine express-handlebar but i get problem is: **TypeError: handlebars is not a function at Object. ** . I have search lot but not any answer to fix. my code is below:
const morgan = require('morgan');
const handlebars = require('express-handlebars');
const app=express();
const port =3000;
// hTTP logger
app.use(morgan('combined'));
//templace engie
app.engine('handlebars',handlebars());
app.set('view engine','handlebars');
app.get('/',(req,res)=> {
return res.send('hello world');
});
app.listen(port,()=>console.log(`Example app listening at http://localhost:${port}`));````
For version 6.0.2, the express-handlebars package exports create, engine function. See source code The basic usage should be:
const { engine } = require("express-handlebars");
const express = require("express");
const app = express();
const port = 3000;
//templace engie
app.engine("handlebars", engine());
app.set("view engine", "handlebars");
app.get("/", (req, res) => {
return res.send("hello world");
});
app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);
router.get('/dist/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
my app.js routing code
app.set('dist', path.join(__dirname, 'dist'));
app.set('view engine', 'html');
app.engine('html', require('hbs').__express);
my folder structure
root/dist/index.html
I cannot open this page on my first page
how can I open this page? Where is my mistake?
thank you for your help
You can use the method set() to redefine express's default settings.
app.set('views', path.join(__dirname, '/dist'));
Working Example
const express = require('express')
const path = require('path')
const app = express()
const port = 3000;
var indexRouter = require('./routes/index');
// Set 'views' directory for any views
// being rendered res.render()
app.set('views', path.join(__dirname, '/dist'));
// Set view engine
app.engine('html', require('hbs').__express);
app.set('view engine', 'html');
app.use('/', indexRouter);
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
routes/index.js
var express = require('express');
var router = express.Router();
router.get('/dist/', function(req, res, next) {
res.render('index', { title: 'Inside Dist Folder Index' });
});
module.exports = router;
I tried connection to database but it has been giving out data as {}, though it is successfully connected and someone told me that error is in my this file, if i want to see what my body parameters it is passing from database then i need to correct this file, so what is that i have to do
const connection = require("./model");
const express = require("express");
const app = express();
const path = require("path");
const expressHandlebars = require("express-handlebars");
const bodyparser = require("body-parser");
const todolist = require("./routes/todo");
app.use(bodyparser.json());
app.set('views', path.join(__dirname, "/views"));
app.engine("hbs", expressHandlebars({
extname: "hbs",
defaultLayout : "mainlayout",
layoutsDir : __dirname + "/views/layouts"
}));
app.use("/todo" , todolist );
app.set("view engine", "hbs")
app.get("/", (req,res) =>{
//res.send("Hello World")
res.render("index",{})
})
app.listen("2000", () =>{
console.log("Server Started");
});
Try doing something like this:
app.get("/", (req,res) =>{
//res.send("Hello World")
res.render("index",{
title:'index page'
})
})
//connection
const PORT = process.env.PORT || 3000
app.listen(PORT, console.log(`listening on port
${PORT}`));
I want to get my news.ejs page to render when i click its link, but i get a error called "Cannot GET /news.ejs"
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
express()
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res) => res.render('pages/index'))
.get('/news', (req, res) => res.render('pages/news'))
.listen(PORT, () => console.log(`Listening on ${ PORT }`))
I have this code and working fine
const express = require('express');
const app = express()
const path = require('path')
const ejs = require('ejs');
app.use(express.static(path.join(__dirname, 'public')))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', ejs)
app.get('/', (req, res) => res.render('privacypolicy.ejs'))
app.get('/news', (req, res) => res.render('rulesnregulations.ejs'))
//app.use('/', indexRouter)
const PORT = 5000;
app.listen(PORT, () => console.log('it started on 5000'))
privacypolicy.ejs & rulesnregulations.ejs these two files are under views folder
Make sure you are linking to /news and not /news.ejs. If you want /news.ejs to be accessible, you will have to change the path that you are registering.