my node virson is s (1)> node --version v14.17.3 npm verson is
i can run my file on the pc ,but when i uplode it to heroku ,ites not working ,and it shows error code like this .
NPM_CONFIG_LOGLEVEL=error NODE_VERBOSE=false NODE_ENV=production NODE_MODULES_CACHE=true
my node virson is s (1)> node --version
v14.17.3
npm verson is
matronics (1)> npm --version
6.14.13
and i have write a nodejs package json like this .
{
"name": "LiveEmbellishedAnimatronics",
"version": "1.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.5",
"express": "^4.17.1",
"express-session": "^1.17.2",
"http-errors": "^1.8.0",
"morgan": "^1.10.0",
"passport": "^0.5.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --hot"
},
"keywords": [],
"author": "",
"license": "ISC"
}
-----------
my app js is like this
var express = require('express'); | |
var exphbs = require('express-handlebars'); | |
var app = express(); | |
//app.engine('.hbs', exphbs({extname: '.hbs'})); | |
//app.set('view engine', '.hbs'); | |
app.engine('hbs', exphbs({defaultLayout: false, extname:'.hbs',})); | |
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"); | |
}); | |
app.set("views", path.join(__dirname, "views")); | |
app.use(express.static(path.join(__dirname, "public"))); | |
var PORT = 3000; | |
app.listen(3000, function () { | |
console.log(`Listening on port ${PORT}`); | |
});
|
Step by step
Solved in 2 steps with 2 images