这是我要发送的发帖请求,但不起作用。我尝试在Postman中使用它以及使用表单,但是错误显示“ can POST / addstudent / add”。下面是代码。
这是JScode:我正在使用express并将其发布到MongoDB中。
app.post('/addstudent/add', function(req, res){
db.collection('students').insertOne(req.body, function(error, result){
if(error)
throw error;
res.json(result);
console.log("New student Successfully Added!");
})
})
这是HTML代码:
<div class="container-fluid">
<div class="formdiv">
<form action="/addstudent/add" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">Student Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter Name" name="Name">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Student Email</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Email" name="Email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Phone Number</label>
<input type="number" class="form-control" id="exampleInputPassword1" placeholder="Phone Number" name="Number">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Age</label>
<input type="number" class="form-control" id="exampleInputPassword1" placeholder="Age" name="Age">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
这是整个JS代码:
var express = require('express');
var bodyParser = require('body-parser');
var mongoDB = require('mongodb').MongoClient;
var hbs = require('hbs');
var app = express();
app.use(express.static(__dirname +'/public'));
app.use( bodyParser.urlencoded());
var url = 'mongodb://localhost:27017';
var db;
mongoDB.connect(url, {useUnifiedTopology: true, useNewUrlParser: true },
function(error, client){
if(error)
throw error;
db = client.db('attainu');
});
app.get('/instructors', function(req, res){
res.render('instructor.hbs');
})
app.get('/students', function(req, res){
res.render('index.hbs');
})
app.post('/addstudent/add', function(req, res){
db.collection('students').insertOne(req.body, function(error, result){
if(error)
throw error;
res.json(result);
console.log("New student Successfully Added!");
})
})
app.listen(3000);
尝试一下
var express = require('express');
var bodyParser = require('body-parser');
var mongoDB = require('mongodb').MongoClient;
var hbs = require('hbs');
const port = process.env.PORT || 3000;
var app = express();
app.use(express.static(__dirname +'/public'));
//Add this line
app.use( bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json())
var url = 'mongodb://localhost:27017';
var db;
mongoDB.connect(url, {useUnifiedTopology: true, useNewUrlParser: true },
function(error, client){
if(error)
throw error;
db = client.db('attainu');
});
app.get('/instructors', function(req, res){
res.render('instructor.hbs');
})
app.get('/students', function(req, res){
res.render('index.hbs');
});
app.post('/addstudent/add', function(req, res){
db.collection('students').insertOne(req.body, function(error, result){
if(error)
throw error;
res.json(result);
console.log("New student Successfully Added!");
});
});
app.listen(port, () => console.log(`Server is running at ${port}`));