简体   繁体   English

Express 服务器不在我的主机上提供 static 文件

[英]Express server doesn't Serve static files on my hosting

Locally is everything okey, and my express.js app is serving my js file, but on my hosting there is a problem.本地一切正常,我的 express.js 应用程序正在为我的 js 文件提供服务,但在我的主机上存在问题。 I can't get my js file.我无法获取我的 js 文件。


var express = require("express")
var app = express()
var path = require("path")
var bodyParser = require('body-parser');
const fs = require('fs');
app.use(express.static('static'))



app.get("/chinczyk", function (req, res) {
    res.sendFile(__dirname + "/index.html") 
})





app.listen();
<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=" initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<div id="formlogin">
    <p>Wpisz swój nick:</p>
<input id="nickname" type="text" name="Wpisz swój nick">
<button id="submit" type="submit">OK</button>
</div>
<script type="text/javascript" src="js/user.js"></script>
<style>

directory of my files on server is like我在服务器上的文件目录就像

public
static > js > user.js
tmp
app.js
index.html

Instead of代替

app.use(express.static('static'))

use利用

app.use(express.static(path.join(__dirname, 'static')));

This will handle any issues arising from the script being executed from a different working directory than you might expect.这将处理从与您预期不同的工作目录执行的脚本引起的任何问题。 It makes the path to the static folder relative to where the executing script lives, not from where it was started.它使 static 文件夹的路径相对于执行脚本所在的位置,而不是从它开始的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM