В моем приложении Node я пытаюсь просмотреть объект request.body, чтобы проверить наличие элементов, которые входят в него, но console.log(req.body)
радуется, что он пуст. Вот мои файлы.
app.js
файл ниже
var express = require("express");
var app = express();
var mongoose = require("mongoose");
var nationalPark = require("./models/NationalParks");
//config
mongoose.connect('mongodb://localhost/National_Parks', {useNewUrlParser: true, useUnifiedTopology: true}).catch(function(reason){
console.log('Unable to connect to the mongodb instance. Error: ', reason);
});
app.set('view engine', 'ejs');
var bod = require("body-parser");
app.use(bod.urlencoded({extended:true}));
app.use(bod.json());
app.use(express.static(__dirname + '/resources'));
//Routes
app.get("/",function(req,res){
nationalPark.find({},function(err,nationalParks){
if(err){
console.log(err);
}else{
res.render("home",{nationalParks:nationalParks});
}
})
});
app.get("/home",function(req,res){
nationalPark.find({},function(err,nationalParks){
if(err){
console.log(err);
}else{
console.log(nationalParks);
res.render("home",{nationalParks:nationalParks});
}
})
});
app.get("/login",function(req,res){
res.render("login");
});
app.get("/signup",function(req,res){
res.render("signup");
});
app.get("/logout",function(req,res){
res.render("home");
});
app.get("/NationalPark",function(req,res){
console.log(park.park)
res.render("NationalPark",{park:park.park});
});
app.post("/NationalPark",function(req,res){
console.log(req.body);
res.render("NationalPark");
});
app.get("/post",function(req,res){
res.render("post");
});
app.get("/post-display",function(req,res){
res.render("post-display");
});
app.get("/profile",function(req,res){
res.render("profile");
});
//listening port
app.listen(3000,function(){
console.log("Server started");
});
Вот мой home.ejs
файл
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link href="https://fonts.googleapis.com/css?family=Pacifico&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="/css/national-parks.css">
</head>
<body>
<div class="container" id="headContainer">
<div class="row">
<div class="col-2">
<nav class="nav flex-column">
<a class="nav-link active text-white" href="/login">Login</a>
<a class="nav-link active text-white" href="/signup">Sign Up</a>
<a class="nav-link active text-white" href="/logout">Logout</a>
</nav>
</div>
<div class="col-10" id="heading">
<h1 class="display-3">National Parks Observer</h1>
<p class="lead">Site Dedicated to Indian National Parks</p>
</div>
</div>
</div>
<div class="container" id="postings">
<% var r=Math.round(nationalParks.length/3); %>
<% var x = 1; %>
<% var elementCount = 3; %>
<% var constant = 3; %>
<% var start=0; %>
<% while(x<=r){ %>
<div class="row">
<% for(var k=start;k<elementCount;k++){ %>
<div class="col-4">
<div class="card">
<form method="POST" action="/NationalPark">
<img class="card-img-top" src=<%=nationalParks[k].imageurl %> alt="Card image cap">
<h5 class="card-title" name="parktitle" id="title"><%=nationalParks[k].nationalPark %></h5>
<button type="submit" class="btn btn-primary btn-sm"">View Page</button>
</form>
</div>
</div>
<% } %>
<% start=start+constant; %>
<% elementCount=Math.min(elementCount+constant,nationalParks.length); %>
</div>
<% x++; } %>
</div>
<br>
<br>
<br>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="/javascript/national-park.js"></script>
</body>
</html>
в следующем методе публикации в app.js
app.post("/NationalPark",function(req,res){
console.log(req.body);
res.render("NationalPark");
});
console.log(req.body)
просто печатает {}
Почему это происходит, я что-то упустил? Буду глубоко признателен за вашу помощь. Заранее спасибо.