简体   繁体   English

使用node.js提供外部静态文件

[英]serving up external static files with node.js

I am using node.JS to build a web app with chat functionality. 我正在使用node.JS构建具有聊天功能的Web应用程序。 My server is serving up the basic content of the page but i'm running into problems importing external files like jpgs and style sheets. 我的服务器正在提供页面的基本内容,但是在导入外部文件(如jpg和样式表)时遇到了问题。 Here is the head of my main page, with the files I want to import: 这是我主页的标题,其中包含我要导入的文件:

<head>
<meta charset="utf-8"/>
 <link rel="stylesheet" type="text/css" href="stylesheets/main.css" />
 <link href='http://fonts.googleapis.com/css?family=Alegreya+Sans' rel='stylesheet' type='text/css'>
 <link href='http://fonts.googleapis.com/css?family=Slabo+27px' rel='stylesheet' type='text/css'>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
 <script src="/socket.io/socket.io.js"></script>
 <script type ="text/javascript">

  var socketio = io.connect();
  socketio.on("message_to_client",function(data) {
     //Append an HR thematic break and the escaped HTML of the new message
     document.getElementById("chatBox").appendChild(document.createElement("hr"));
     document.getElementById("chatBox").appendChild(document.createTextNode(data['message']));
  });

  function sendMessage(){
     var msg = document.getElementById("message_input").value;
     socketio.emit("message_to_server", {message:msg});
  }

This is the chat server I use to serve up the main page and handle messages. 这是我用来提供主页和处理消息的聊天服务器。 I'm stuck on getting it to handle static resources: 我一直坚持让它处理静态资源:

var http = require("http"),
socketio = require("socket.io"),
fs = require("fs"),
mime = require('mime');

var url = 'home.html';


var app = http.createServer(function(req, resp){

fs.readFile(url, function(err, data){

    if(err) return resp.writeHead(500);

    var mimeType = mime.lookup(url);
    resp.setHeader('Content-Type', mimeType);
    resp.writeHead(200);
    resp.end(data);
    });
});
app.listen(3456);

var io = socketio.listen(app);
io.sockets.on("connection", function(socket){
// This callback runs when a new Socket.IO connection is established.

socket.on('message_to_server', function(data) {
    // This callback runs when the server receives a new message from the client.

    console.log("message: "+data["message"]); // log it to the Node.JS output
    io.sockets.emit("message_to_client",{message:data["message"] }) // broadcast the message to other users
 });
});

In order to serve static files, do this (I use express, so I add a middleware to do that) 为了提供静态文件,请执行此操作(我使用express,因此我添加了一个中间件来执行此操作)

In your main app file, add this middleware: 在您的主应用程序文件中,添加以下中间件:

app.use(express.static(__dirname + '/public')); //the argument is the location of the root directory with static files

Actually that's it. 其实就是这样。 Your clients will be served the static files hosted in path /public 将向您的客户提供路径/public托管的静态文件

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

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