简体   繁体   English

在node.js中从何处以及如何服务文件

[英]where and how to serve files from , in node.js

I am new to node.js/express.js and I am reading some tutorials. 我是node.js / express.js的新手,正在阅读一些教程。 I am confused because I am used to the simple apache logic, node.js/express.js logic confuses me. 我很困惑,因为我习惯于简单的Apache逻辑,所以node.js / express.js逻辑使我感到困惑。 Please help me. 请帮我。

This tutorial uses the default express routes to add/get data from a db. 教程使用默认的快速路由从数据库添加/获取数据。 But, at the begging , at the part named "PART 2 – OK, FINE, LET'S DO "HELLO, WORLD!" edits the ...\\ROUTES\\INDEX.JS file to add just a simple html page. Why is that? 但是,在乞求时,在名为“ PART 2 – OK,FINE,让我们做” HELLO,WORLD!”的部分中,编辑...\\ROUTES\\INDEX.JS文件仅添加一个简单的html页面。为什么?

  1. Can I just use the public folder to serve my files and access the by using the same URL? 我可以只使用public文件夹为我的文件,并使用相同的URL访问?

  2. If I have like 50 files, I have to add 50 similar functions to my ...\\ROUTES\\INDEX.JS so I can serve them ? 如果我喜欢50个文件,则必须向...\\ROUTES\\INDEX.JS添加50个类似的函数,以便为他们提供服务? Even the simplest static files ? 即使是最简单的静态文件?

  3. Can I just put all my files in the public folder and then edit app.js and ...\\ROUTES\\INDEX.JS ? 可我只是把我的所有文件public文件夹,然后编辑app.js...\\ROUTES\\INDEX.JS

Also I was reading the first chapter of the book Jump Start Node.js by Don Nguyen. 另外,我正在阅读Don Nguyen的《 Jump Start Node.js 》一书的第一章。 It does not edit routes, just adds methods to the app.js and implements new modules (named db and user ) for adding users to the db. 它不编辑路由,仅将方法添加到app.js并实现用于将用户添加到db的新模块(名为dbuser )。 This also adds a new get function to app.js for a simple form.html file. 这也为简单的form.html文件向app.js添加了一个新的get函数。

  1. Again, why can I use the public folder and then edit the app.js and create my own modules? 同样,我为什么可以使用public文件夹,然后编辑app.js和创建自己的模块?

  2. Again, If I have like 50 files, I have to add 50 similar functions to my app.js so I can serve them ? 同样,如果我喜欢50个文件,则必须向app.js添加50个类似的函数,以便为他们提供服务? Even the simplest static files ? 即使是最简单的静态文件?

    Finally, 最后,

  3. What is the difference between the two methods ? 两种方法有什么区别? In which case I use them ? 在这种情况下,我会使用它们?

  4. Which one is the best practice ? 最佳做法是哪一种?

Thank you very much 非常感谢你

To serve the folder named "public" as static files: 要将名为“ public”的文件夹用作静态文件:

app.use(express.static(__dirname + '/public'));

The reason the tutorial did not put their 'simple' index page into public , is that their 'simple' page is not static . 本教程之所以未将其“简单”索引页面public ,是因为其“简单”页面不是静态的 They pass in the data { title: 'Express' } to the dynamic page. 它们将数据{ title: 'Express' }传递到动态页面。

If the title 'Express' is always going to be static, then yes you can serve it from public . 如果标题“ Express”始终是静态的,那么可以从public However for the sake of the tutorial, we assume they might dynamically change the title from 'Express' to something else. 但是,为了本教程的缘故,我们假设他们可以将标题从“ Express”动态更改为其他名称。

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

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