简体   繁体   English

使用Node.js和Socket.io查找文件

[英]Locating files using Node.js and Socket.io

I am having trouble with local includes on the client side using Node.js and Socket.io. 我在使用Node.js和Socket.io的客户端上使用本地包含项时遇到麻烦。 This may be to my PHP/Apache mindset I have had for file requests for most of my life. 这可能是我一生中对文件请求的PHP / Apache心态。

On my server, I load the page likewise: 在我的服务器上,我同样加载页面:

var express = require("express");
var app = express();
var path = require("path");
var server = require("http").createServer(app);
var io = require("socket.io")(server);
var mysql = require("mysql");

var port = process.env.PORT;
var ip = process.env.IP;

app.use(express.static(__dirname + "/client"));

//start opening socket connection handlers ...

And my files are organized likewise: 我的文件也按以下方式组织:

  • games 游戏
    • libraries 图书馆
      • bigInt bigInt
      • threejs threejs
      • etc... 等等...
    • version_1 版本_1
      • client 客户
        • index.html index.html
        • index.js index.js
        • index.css index.css
      • server.js server.js
      • database.sql 数据库.sql
    • version_2 版本_2
    • version_3 版本_3
    • etc... 等等...

Depending which version I want to run, I open that version's directory and run its server.js file. 根据要运行的版本,打开该版本的目录并运行其server.js文件。 The line redirects the client to /client/index.html with the line app.use(express.static(__dirname + "/client")) . 该行通过app.use(express.static(__dirname + "/client"))客户端重定向到/client/index.html。 But now only files that are in the client folder are reachable by <script></script> or <link> tags but not those libraries in the libraries folder that I use across versions. 但是现在, <script></script><link>标记只能访问客户端文件夹中的文件,而我跨版本使用的库文件夹中的那些库则无法访问。

How do I change my code to be able to access files inside the libraries folder from /version_x/client/index.html while still directing the client to proper html file? 如何更改代码以能够从/version_x/client/index.html访问library文件夹中的文件,同时仍将客户端定向到正确的html文件?

Note: Due to this issue, I have been forced to use only libraries with supported CDNs for the past couple weeks I have been learning Node.js. 注意:由于这个问题,在过去学习我的Node.js的过去两周里,我被迫仅使用具有受支持CDN的库。

Add the following line right after var ip = process.env.IP; var ip = process.env.IP;之后添加以下行var ip = process.env.IP; :

app.use('/libraries', express.static(path.join(__dirname, '..', 'libraries'));

What this does is adding a new route to your application server. 这是向您的应用程序服务器添加一条新路由。 All your files inside your /games/libraries folder are now accessible via /libraries . 您现在可以通过/libraries访问/games/libraries文件夹中的所有文件。

How does it work? 它是如何工作的? Your express router uses different middlewares based on the provided paths. express路由器根据提供的路径使用不同的中间件。 This line tells the router, to use the static middleware and serve files from ../libraries when a HTTP Request for anything under /libraries comes in. 此行告诉路由器,当对/libraries下的任何内容发出HTTP请求时,使用static中间件并为../libraries提供文件。

You can serve more folders with express.static 您可以使用express.static提供更多文件夹

//Serve Client Folder
app.use(express.static(__dirname + "/client"));

//Server External Libraries Folder
app.use('/libs', express.static(__dirname + "/../libraries"));


 //Ex: <script src="libs/threejs/threejs.js">
 //Will load libraries/threejs/threejs.js

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

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