简体   繁体   English

将 vanilla JS 转换为与 Nodejs、Mongo 和 Express 一起使用

[英]convert vanilla JS to work with Nodejs, Mongo, and Express

I am working on a blog project for a class using Nodejs, Mongo, and Express.我正在使用 Nodejs、Mongo 和 Express 为 class 开发一个博客项目。 We have been building up to using Express.我们一直在努力使用 Express。 I had previous JS code to create a slideshow for blog posts.我有以前的 JS 代码来为博客文章创建幻灯片。 I now need the slideshow to access data from MongoDB and route to a post.ejs file with the matching title (so it can find it on the database and show it on the post.ejs file).我现在需要幻灯片来访问来自 MongoDB 的数据并路由到具有匹配标题的 post.ejs 文件(因此它可以在数据库中找到它并在 post.ejs 文件中显示它)。 I cannot use the slideshow buttons because as I mentioned before that was vanilla js used before being introduced to Node.我不能使用幻灯片按钮,因为正如我之前提到的,这是在引入 Node.js 之前使用的 vanilla js。 How can I convert the old vanilla JS to work with express and pull data from Mongo DB?如何将旧的 vanilla JS 转换为使用 express 并从 Mongo DB 中提取数据? I have managed to learn a lot on my own but I'm stuck now.我已经设法自己学到了很多东西,但我现在陷入了困境。

Here it my GitHub repository.这是我的 GitHub 存储库。https://github.com/mmercad4/Marco_800951815/tree/milestone-5https://github.com/mmercad4/Marco_800951815/tree/milestone-5

The old JS is in /public/assets/main.js旧 JS 在 /public/assets/main.js

My post controller is in controllers/postController.js我的帖子 controller 在 controllers/postController.js

My jquery folder for ajax is in public/assets/jquery.js我的 ajax 的 jquery 文件夹位于 public/assets/jquery.js

And finally my index.ejs and post.ejs are located in the views folder.最后我的 index.ejs 和 post.ejs 位于 views 文件夹中。

You can modify your vanilla JS files and make them into ejs templates.您可以修改您的 vanilla JS 文件并将它们制作成 ejs 模板。 Use the same syntax that you used for post.ejs so that you can access the data you pass in.使用与post.ejs相同的语法,以便您可以访问传入的数据。

On your server you should set up a route handler such as app.get("/example") .在您的服务器上,您应该设置一个路由处理程序,例如app.get("/example") Inside of your route handler, you should query your mongodb instance for the data you need.在路由处理程序内部,您应该查询 mongodb 实例以获取所需的数据。 Now take your newly modified ejs file and render it, passing in the result of the query from mongodb.现在获取新修改的 ejs 文件并渲染它,传入来自 mongodb 的查询结果。 Here's an example:这是一个例子:

app.get("/example", (req, res) => {
    const data = getDataFromMongo() // insert your query here
    res.render("main", data);
}

If you want your client to be responsible for fetching the data instead, then skip transforming your vanilla files into ejs, and you can simply return the mongodb data as JSON like so:如果您希望您的客户端负责获取数据,然后跳过将您的 vanilla 文件转换为 ejs,您可以简单地将 mongodb 数据作为 JSON 返回,如下所示:

app.get("/example", (req, res) => {
    const data = getDataFromMongo() // insert your query here
    res.json(data);
}

Now in your client side code, make an ajax GET request to "/example" and you can use the JSON returned however you wish.现在,在您的客户端代码中,向“/example”发出 ajax GET 请求,您可以使用返回的 JSON 来满足您的需求。 No need for ejs template syntax here.这里不需要 ejs 模板语法。

So it's up to you to how you want to control access to your data.因此,您可以自行决定如何控制对数据的访问。 The first method handles everything server-side.第一种方法在服务器端处理一切。 The second method splits responsibility between server and client.第二种方法在服务器和客户端之间划分责任。 In this case, the server acts as more of a data API than a rendering engine.在这种情况下,服务器更多地充当数据 API 而非渲染引擎。

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

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