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 ?
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 ?
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images