简体   繁体   English

如何在 node.js 中使用 Javascript 获取发布请求的时间

[英]How to get the time a post request was made using Javascript in node.js

I'm making a basic blog application(I'm a newcomer to web development,so I'm building thisfor learning reasons, so nothing advanced), and when someone needs to publish something they head over to a"/compose" route.我正在制作一个基本的博客应用程序(我是 Web 开发的新手,所以我出于学习原因构建它,所以没有任何高级内容),并且当有人需要发布某些内容时,他们会转到“/撰写”路线。 And when they publish a POST request is made to the server like this,当他们像这样向服务器发布 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);
            }
        });
});

and it works fine as of now(I also use Body-Parser. But I also need to know the time the request was made so I can include the written time in the Blog Post. How can I go about achieving it?并且它现在工作正常(我也使用 Body-Parser。但我还需要知道发出请求的时间,以便我可以在博客文章中包含书面时间。我该如何实现它?

If you're using mongoose , you can simply add an extra property to your schema:如果您使用的是mongoose ,您可以简单地向您的架构添加一个额外的属性:

const { Schema } = require("mongoose");

const MovieSchema = new Schema({
    title: String,
    content: String,
    category: String,
    author: String,
    date: { type: Date: default: () => new Date() }
});

This will automatically add the date to the new document when it is saved to the database, so you don't have to do it manually.这将在新文档保存到数据库时自动将日期添加到新文档中,因此您不必手动执行此操作。

I think I figured it out , I did it like this(I also updated the post Schema.),我想我想通了,我是这样做的(我也更新了帖子架构。),

    const postCategory = req.body.postCategory;
    const postBody = req.body.postBody;
    const authorName = req.body.authorName;
    const addedDate = new Date();
    const addedTime = addedDate.toString();

    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,
                        time : addedTime
                    });
                    newPost.save(function(err){
                        if(!err){
                            console.log(addedTime);
                            res.redirect("/");
                        }
                    });
                } else {
                    res.send("Post title already exists!Revisit the compose page to publish anything else.")
                }
            } else {
                console.log(err);
            }
        });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM