簡體   English   中英

將服務器端存儲的javascript文件添加到html(node.js)

[英]Add a serverside stored javascript file to html (node.js)

我的項目有一個小問題。 我正在使用node並表示一個小的網頁..它正在呈現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問候

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

例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM