【问题标题】:Add a serverside stored javascript file to html (node.js)将服务器端存储的 javascript 文件添加到 html (node.js)
【发布时间】:2014-01-23 15:18:57
【问题描述】:

我的项目有一个小问题。 我正在为一个小网页使用 node 和 express .. 它正在呈现 html 页面,但是 javascript 文件(test.js)没有发送给用户..

<!doctype html>
<html>
    <head>
        <title>Spektrum</title>
        <script src="test.js"></script>
        <meta name = "viewport" content = "initial-scale = 1, user-scalable = no">
        <style>
            canvas{     

            }
        </style>
    </head>
    <body>    
        <!-- some stuff --> 
        </body> 
</html>  

Node.js 文件:

var http = require('http'),  
    fs = require('fs'),
    express = require('express'), 
    app     = express(); 
    app.use(express.bodyParser());
    app.set('view engine', 'ejs');
    app.engine('html', require('ejs').renderFile);

// ..... 
    app.listen(8080, function() {
  console.log('Server running at http://127.0.0.1:8080/');
}); 

知道如何将带有 html 文件的 javascript 文件发送给用户吗?

您好, JS

【问题讨论】:

标签: javascript html node.js express


【解决方案1】:

您需要使用static 中间件来提供文件(test.js 需要存在)

示例:

// GET /javascripts/jquery.js
// GET /style.css
// GET /favicon.ico
app.use(express.static(__dirname + '/public'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多