【发布时间】:2021-01-19 05:55:23
【问题描述】:
我正在制作一个基本的博客应用程序(我是 Web 开发的新手,所以出于学习的原因,我正在构建它,所以没有什么高级的),当有人需要发布一些东西时,他们会前往“/compose “ 路线。当他们像这样向服务器发出 POST 请求时,
app.post("/compose",function(req,res){
const postTitle = req.body.postTitle;
const postCategory = req.body.postCategory;
const postBody = req.body.postBody;
const authorName = req.body.authorName;
if (postCategory === "movies"){
MoviePost.findOne({title: postTitle}, function(err, foundPost){
if(!err){
if (!foundPost){
const newPost = new MoviePost({
title: postTitle,
content: postBody,
category: postCategory,
author: authorName
});
newPost.save(function(err){
if(!err){
res.redirect("/");
}
});
} else {
res.send("Post title already exists!Revisit the compose page to publish anything else.")
}
} else {
console.log(err);
}
});
});
到目前为止它工作正常(我也使用 Body-Parser。但我还需要知道提出请求的时间,以便我可以在博客文章中包含书面时间。我该如何实现它?
【问题讨论】:
-
像
Date.now()这样简单吗? -
我弄明白了,谢谢大家的帮助。我是这样弄的,const addedDate = new Date(); const addedTime = addedDate.toString();
标签: javascript node.js express ejs body-parser