在node.js应用的客户端导入JS模块



在node.js应用的客户端导入JS模块的标准方式是什么?

添加<script src="..."></script>似乎很糟糕,但我不知道我还能做什么。

更新:

服务器的代码如下:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
  res.sendfile('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');
});

客户端:

<!doctype html>
<html>
  <head>
    <title>Website</title>
    <script src="jquery.js"></script> (doesn't work)
  </head>
  <body>
  </body>
</html>

在index.js中这样做:

var module = require('module');
exports.index = function(req, res) {
    res.render('index', { module : module});
    //Now your EJS will have the module
}

最新更新