i have take  a  node js file below  app.js  const express = require("express"); const app = express(); const bodyParser = require("body-parser"); const fs = require("fs"); var createError = require("http-errors"); const passport = require("passport"); var path = require("path"); const session = require("express-session"); var cookieParser = require("cookie-parser"); var logger = require("morgan"); var express = require('express'); var exphbs = require('express-handlebars'); 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(express.static("./public")); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); var userLogin = {}; app.post("/api/login", (req, res) => { fs.readFile("./data.json", (err, data) => { var arr = []; if (err) { console.log(err); } else { if (data.toString()) { arr = JSON.parse(data.toString()); } var s = arr.find((item) => { if (item.name == req.body.name) { return item; } }); if (s) { if (s.password == req.body.password) { userLogin = req.body; res.json({ status: "y", meg: "login success", data: s.name, }); } else { res.json({ status: "err", meg: "wrong password ", }); } } else { res.json({ status: "n", meg: "no such user ", }); } } }); }); app.get("/index", (req, res) => { if (userLogin.name) res.render("index", { username: userLogin.name }); else res.render("login"); }); app.get("/", (req, res) => { res.render("login"); }); var PORT = 3000; app.listen(3000, function () { console.log(`Listening on port ${PORT}`); }); -------------------------------------     if i add "var express = require('express');"  it hsows error " node app.js V:\ga2\moban4943\k1\app.js:11 var express = require('express'); ^ SyntaxError: Identifier 'express' has already been declared ?[90m at wrapSafe (internal/modules/cjs/loader.js:1001:16)?[39m ?[90m at Module._compile (internal/modules/cjs/loader.js:1049:27)?[39m ?[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:1 0)?[39m ?[90m at Module.load (internal/modules/cjs/loader.js:950:32)?[39m ?[90m at Function.Module._load (internal/modules/cjs/loader.js:790:14)?[39m ?[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_ma in.js:76:12)?[39m ?[90m at internal/main/run_main_module.js:17:47?[39m    if i delete the code "var express = require('express');" then it shows "Error: Cannot find module 'hbs' Require stack: - V:\ga2\moban4943\k1\node_modules\express\lib\view.js - V:\ga2\moban4943\k1\node_modules\express\lib\application.js - V:\ga2\moban4943\k1\node_modules\express\lib\express.js - V:\ga2\moban4943\k1\node_modules\express\index.js - V:\ga2\moban4943\k1\app.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15) at Function.Module._load (internal/modules/cjs/loader.js:746:27) at Module.require (internal/modules/cjs/loader.js:974:19) at require (internal/modules/cjs/helpers.js:92:18) at new View (V:\ga2\moban4943\k1\node_modules\express\lib\view.js:42:49) at Function.app.render (V:\ga2\moban4943\k1\node_modules\express\lib\application.js:487:12) at ServerResponse.res.render (V:\ga2\moban4943\k1\node_modules\express\lib\response.js:717:7) at V:\ga2\moban4943\k1\app.js:67:5 at callbacks (V:\ga2\moban4943\k1\node_modules\express\lib\router\index.js:162:37) at param (V:\ga2\moban4943\k1\node_modules\express\lib\router\index.js:136:11)  whats  this matter ? how can i fix this code ?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 i have take  a  node js file below 

app.js 

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const fs = require("fs");
var createError = require("http-errors");
const passport = require("passport");
var path = require("path");
const session = require("express-session");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var express = require('express');
var exphbs = require('express-handlebars');
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(express.static("./public"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var userLogin = {};
app.post("/api/login", (req, res) => {
fs.readFile("./data.json", (err, data) => {
var arr = [];
if (err) {
console.log(err);
} else {
if (data.toString()) {
arr = JSON.parse(data.toString());
}
var s = arr.find((item) => {
if (item.name == req.body.name) {
return item;
}
});
if (s) {
if (s.password == req.body.password) {
userLogin = req.body;
res.json({
status: "y",
meg: "login success",
data: s.name,
});
} else {
res.json({
status: "err",
meg: "wrong password ",
});
}
} else {
res.json({
status: "n",
meg: "no such user ",
});
}
}
});
});

app.get("/index", (req, res) => {
if (userLogin.name) res.render("index", { username: userLogin.name });
else res.render("login");
});

app.get("/", (req, res) => {
res.render("login");
});

var PORT = 3000;
app.listen(3000, function () {
console.log(`Listening on port ${PORT}`);
});

-------------------------------------

 

 

if i add "var express = require('express');" 

it hsows error "

node app.js
V:\ga2\moban4943\k1\app.js:11
var express = require('express');
^

SyntaxError: Identifier 'express' has already been declared
?[90m at wrapSafe (internal/modules/cjs/loader.js:1001:16)?[39m
?[90m at Module._compile (internal/modules/cjs/loader.js:1049:27)?[39m
?[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:1
0)?[39m
?[90m at Module.load (internal/modules/cjs/loader.js:950:32)?[39m
?[90m at Function.Module._load (internal/modules/cjs/loader.js:790:14)?[39m
?[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_ma
in.js:76:12)?[39m
?[90m at internal/main/run_main_module.js:17:47?[39m

 

 if i delete the code "var express = require('express');"

then it shows "Error: Cannot find module 'hbs' Require stack: - V:\ga2\moban4943\k1\node_modules\express\lib\view.js - V:\ga2\moban4943\k1\node_modules\express\lib\application.js - V:\ga2\moban4943\k1\node_modules\express\lib\express.js - V:\ga2\moban4943\k1\node_modules\express\index.js - V:\ga2\moban4943\k1\app.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15) at Function.Module._load (internal/modules/cjs/loader.js:746:27) at Module.require (internal/modules/cjs/loader.js:974:19) at require (internal/modules/cjs/helpers.js:92:18) at new View (V:\ga2\moban4943\k1\node_modules\express\lib\view.js:42:49) at Function.app.render (V:\ga2\moban4943\k1\node_modules\express\lib\application.js:487:12) at ServerResponse.res.render (V:\ga2\moban4943\k1\node_modules\express\lib\response.js:717:7) at V:\ga2\moban4943\k1\app.js:67:5 at callbacks (V:\ga2\moban4943\k1\node_modules\express\lib\router\index.js:162:37) at param (V:\ga2\moban4943\k1\node_modules\express\lib\router\index.js:136:11)

 whats  this matter ? how can i fix this code ?

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education