POST request to server gives {} as output in terminal - node.js

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}`));

Related

exphbs is not a function

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);

Express-handlebars error - HandleBars is not a function

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}`);
});

TypeError: handlebars is not a function error

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}`)
);

Express app: Error: Failed to lookup view "index" in views directory "views"

I am working along with a tutorial to learn MVC and I am unable to get my views to render. When I try to render my views locally I receive the following error. I'm not well versed in serving views from express, so I'm not sure about what steps to take in trouble shooting this error.
Error: Failed to lookup view "index" in views directory "views"
My app.js file reads as follows:
const path = require("path")
const express = require('express');
const app = express()
const port = process.env.port || 3000
// express middelware
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.use(express.static("public"))
// views are the files you render
app.set("views", "views")
app.set("view engine", "hbs")
// end express middleware
app.get("/", (req, res) => {
res.render("index")
})
app.get("/about", (req, res) => {
res.render("about")
})
app.listen(port, () => {
console.log(`listening on port ${port}`)
})
My file setup is as follows:
const path = require("path")
const express = require('express');
const app = express()
const port = process.env.port || 3000
// express middelware
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.use(express.static("public"))
// views are the files you render
app.set('views', __dirname + '/../views');
app.set("view engine", "hbs")
// end express middleware
app.get("/", (req, res) => {
res.render("index")
})
app.get("/about", (req, res) => {
res.render("about")
})
app.listen(port, () => {
console.log(`listening on port ${port}`)
})
You need to reference the views folder as part of the directory path.
app.set('views', __dirname + '/../views');

Cant get ejs page to render

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.

Resources