Heroku正在显示
"Internal server error"
当我点击“ ../save”路线时。
我尝试在localhost上运行代码以检查错误,并且得到以下结果。
这是我的代码-
index.js
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use('/public',express.static('public'));
app.set("view engine","ejs");
var port=process.env.PORT || 3000;
items = {}
obj1 = {
name : "Gaurav Kumar Sharma",
sentFrom : "Server",
msg : "Index Page",
time : new Date().toLocaleTimeString()
}
obj2 = {
name : "Gaurav Sharma",
sentFrom : "Server",
msg : "Error 404",
time : new Date().toLocaleTimeString()
}
var orgName = ""
var address = ""
//Items will be added here
app.get("/save",function(req,res){
res.render("canvas",{orgName : orgName,address:address,items:items})
})
app.post("/savetodatabase",function(req,res){
//console.log(req.body);
obj1.msg = req.body.imgURL;
res.redirect("/");
})
app.get("*",function(req,res){
res.send("PAGE RENDERED!!, Go Back and save it.");
})
app.listen(port,function(){
console.log("Server Started at port " + port);
})
Canvas.ejs
<html>
<head></head>
<body>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<canvas id="myCanvas" width="640" height="840" style="border:1px solid black;"></canvas>
<form id="getForm" action="/savetodatabase" method="POST">
<input type="text" name="imgURL" id="imgURL">
<button id="submit">Confirm</button>
</form>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
....
....
start = 170
i=1
'<%items.forEach(function(item){%>'
ctx.fillText(i+".",40,start)
ctx.fillText('<%=item.name%>',110,start)
ctx.fillText('<%=item.rate%>',370,start)
ctx.fillText('<%=item.quantity%>',440,start)
ctx.fillText('<%=item.amount%>',530,start)
i = i + 1
start = start + 30
'<%})%>'
.....
.....
var img = canvas.toDataURL();
$("#imgURL").val(img);
</script>
</body>
</html>
我打“ ../save”路线时出现错误-
TypeError: /home/gaurav/Downloads/New Project/saveCanvas(azure)/views/canvas.ejs:28
26| start = 170
27| i=1
>> 28| '<%items.forEach(function(item){%>'
29| ctx.fillText(i+".",40,start)
30| ctx.fillText('<%=item.name%>',110,start)
31| ctx.fillText('<%=item.rate%>',370,start)
items.forEach is not a function
at eval (eval at compile (/home/gaurav/Downloads/New Project/saveCanvas(azure)/node_modules/ejs/lib/ejs.js:633:12), <anonymous>:17:13)
at returnedFn (/home/gaurav/Downloads/New Project/saveCanvas(azure)/node_modules/ejs/lib/ejs.js:668:17)
at tryHandleCache (/home/gaurav/Downloads/New Project/saveCanvas(azure)/node_modules/ejs/lib/ejs.js:254:36)
at View.exports.renderFile [as engine] (/home/gaurav/Downloads/New Project/saveCanvas(azure)/node_modules/ejs/lib/ejs.js:485:10)
at View.render (/home/gaurav/Downloads/New Project/saveCanvas(azure)/node_modules/express/lib/view.js:135:8)
at tryRender (/home/gaurav/Downloads/New Project/saveCanvas(azure)/node_modules/express/lib/application.js:640:10)
at Function.render (/home/gaurav/Downloads/New Project/saveCanvas(azure)/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/gaurav/Downloads/New Project/saveCanvas(azure)/node_modules/express/lib/response.js:1012:7)
at /home/gaurav/Downloads/New Project/saveCanvas(azure)/app.js:65:9
at Layer.handle [as handle_request] (/home/gaurav/Downloads/New Project/saveCanvas(azure)/node_modules/express/lib/router/layer.js:95:5)
控制台中显示的错误是-
我应该期待一个渲染的画布,其中包含我在路线中发送的数据-
res.render("canvas",{orgName : orgName,address:address,items:items})
帮我解决错误。