简体   繁体   English

从外部连接时如何将外部文件包含到node js项目中

[英]How to include external files to node js project while connecting from outside

server.js server.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

index.html index.html

<!doctype html>
<html>
  <head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Socket.IO chat</title>
    <link href="style.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <div id="chat">
        <ul id="messages"></ul>
        <input id="m" autocomplete="off">
        <div id="button">Send</div>
    </div>

    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io();
      $('#button').on('click touchstart', function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });
    </script>
  </body>
</html>

I start my server on my desktop PC runing windows 8.0 x64 and i forward port on router so then i connect for example from my phone going to xxx.xxx.xxx.xxx:3000 (current home ip address) all was working ok until i wanted to include external jquery from my "server" and css file style.css also from the server. 我在运行Windows 8.0 x64的台式PC上启动服务器,并在路由器上转发端口,因此例如我从手机连接到xxx.xxx.xxx.xxx:3000(当前家庭IP地址),一切正常,直到我希望包括来自我的“服务器”的外部jquery和来自服务器的css文件style.css。 I was reading on stack that there is some method called static path so i created inside my project folder "public" and i used: 我在堆栈上读到,有一种称为静态路径的方法,因此我在项目文件夹“ public”中创建了该文件,并使用了:

app.use(express.static(path.join(__dirname, 'public'))); which i found here: Node.js - external JS and CSS files (just using node.js not express) 我在这里找到的: Node.js-外部JS和CSS文件(仅使用node.js不表达)

but not single one method presented on stack worked, it keep returning several errors: 但是在堆栈上介绍的一种方法并不是可行的,它会不断返回一些错误:

is not an object is not defined or just server starts but file is not found is not defined is not an object或只是服务器启动但找不到文件

my hd path is: 我的高清路径是:

C:\Users\Patryk\Desktop\nodeproject\index.html
C:\Users\Patryk\Desktop\nodeproject\server.js
C:\Users\Patryk\Desktop\nodeproject\node_modules\...
C:\Users\Patryk\Desktop\nodeproject\public\...

It sounds like you might not have things in the proper order. 听起来您可能没有正确的顺序。 You need to declare your static paths before your routes. 您需要在路由之前声明静态路径。 Something like this: 像这样:

var express = require('express');
var app = express();

app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

Here is an updated version of your server.js file: 这是server.js文件的更新版本:

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

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

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