【问题标题】:How to render all js file in server with nodejs如何使用nodejs渲染服务器中的所有js文件
【发布时间】:2013-02-28 14:46:16
【问题描述】:

我已经使用带有 jquery 的 html 设计了我的网页。我的网页加载非常慢,因为我包含更多的 js 库。如果我将执行一些动态操作,它将调用 javascript 库并在客户端进行一些操作,因此加载速度很慢.

现在我来回答我的问题:

我想在服务器端使用 nodejs 编译我的所有 javascript 文件。我必须只向客户端提供 html 输出,因为客户端不会进行任何操作,它只会显示 html。

我想要这样:

1.如果服务器请求页面 2.请求转到nodejs服务器 3.nodejs应该编译所有链接的js,css 4.最终它将动态内容渲染成单个html 5.返回浏览器 6.浏览器会显示html文件

我不确定是否可以在 nodejs 中使用,因为我是这个 nodejs 技术的初学者。如果你能提前感谢,请帮助我。

如果我的问题是错误的,请原谅我

【问题讨论】:

    标签: jquery html node.js


    【解决方案1】:
    var express = require('express')
      , app = express()
      , http = require('http')
      , server = http.createServer(app);
    
    server.listen(3000);
    console.log('Server Started @ 3000');
    
    app.get('/', function (req, res) {
        res.sendfile(__dirname + '/index.html');
    });
    app.get('/index.html', function (req, res) {
        res.sendfile(__dirname + '/index.html');
    });
    app.get('/game.html', function (req, res) {
        res.sendfile(__dirname + '/game.html');
    });
    app.get('/:url', function (req, res) {  
        res.sendfile(__dirname + req.url);
    });
    app.get('/:url[.js]', function (req, res) {
        res.sendfile(__dirname + req.url);
    });
    app.get('/:url[.css]', function (req, res) {    
        res.sendfile(__dirname + req.url);
    });
    app.get('/images/:url[.png]', function (req, res) {
        res.sendfile(__dirname + req.url);
    });
    

    试试这个希望你能有所了解

    如果我的回答错了请见谅

    【讨论】:

      【解决方案2】:

      IMO,您需要一个 Node.js Web 应用程序框架。网上有很多。

      一些例子:

      【讨论】:

        猜你喜欢
        • 2016-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-20
        • 2016-05-22
        • 2015-01-17
        相关资源
        最近更新 更多