const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require('mongoose');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
mongoose.set('useUnifiedTopology', true);
mongoose.connect("mongodb://localhost:27017/restDB",{useNewUrlParser:true});
const articleSchema={
title:String,
content:String
}
const Article = mongoose.model("Article",articleSchema);
app.get("/articles",function(req,res){
Article.find(function(err,foundArticles){
if(!err){
res.send(foundArticles)
}else{
res.send(err)
}
});
});
app.post("/articles",function(req,res){
const newArticle = Article({
title: req.body.title,
content: req.body.content
});
newArticle.save(function(err){
if(!err){
res.send("succes")
}else{
res.send(err)
}
})
app.delete("/articles",function(req,res){
Article.deleteMany(function(err){
if(!err){
res.send("success")
}else{
res.send(err)
}
});
});
app.listen(3000, function() {
console.log("Server started on port 3000");
});
i am making request through postman the same code was running fine but it started giving this error before this error code was doing fine and i dont understand whats happening
SyntaxError: Unexpected end of input
at wrapSafe (internal/modules/cjs/loader.js:1071:16)
at Module._compile (internal/modules/cjs/loader.js:1121:27)
at internal/main/run_main_module.js:18:47
please help me with that it doest include html file i am making requst with postman and using mongodb as a database
Please recheck your closing bracket
your api with endpoint articles isn't closed.
app.post("/articles", function (req, res) {
const newArticle = Article({
title: req.body.title,
content: req.body.content
});
newArticle.save(function (err) {
if (!err) {
res.send("succes")
} else {
res.send(err)
}
});
});
A SyntaxError: Unexpected end of input error
typically means either you are missing a closing parenthesis, brace, bracket, or similar character somewhere in your code or you are trying to JSON.parse()
a string that is missing such characters.
Run your code/JSON string through a javascript linter (eg jshint) to find syntax errors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.