将服务器端存储的javascript文件添加到html(node.js)

时间:2022-03-27 18:58:47

I'm having a small problem with my project. I'm using node and express for a little webpage .. it is rendering the html page, but the javascript file (test.js) is not sent to the user ..

我的项目遇到了一个小问题。我正在使用节点和表达一个小网页..它正在渲染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 file:

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/');
}); 

Any idea how to send the javascript file with the html file to the user?

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

Greetings, JS

问候,JS

1 个解决方案

#1


3  

you need to use static middleware to serve file (test.js needs to exist)

你需要使用静态中间件来提供文件(test.js需要存在)

example:

例:

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

#1


3  

you need to use static middleware to serve file (test.js needs to exist)

你需要使用静态中间件来提供文件(test.js需要存在)

example:

例:

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