簡體   English   中英

Express static 適用於 index.html 但不適用於其他 static 頁面

[英]Express static works for index.html but not other static pages

我正在 NodeJS 中設置一個基本服務器,出於某種原因,當告訴 Express 將public用於 static 個文件時,只有index.html被成功加載。 所以,我的/路線有效,但/pageone/pagetwo路線返回

Error: ENOENT: no such file or directory, stat '/page1.html'

這是我的文件結構:

.
├── public
│   ├── index.html
│   └── client.js
|   └── page1.html
|   └── page2.html
├── app.js

這是我的文件:

應用程序.js

const express = require('express')
const app = express()
app.use(express.static('public'))

app.get('/', function(req, res) {
    res.sendFile('/index.html');
});

app.get('/pageone', function(req, res) {
    res.sendFile('/page1.html');
});

app.get('/pagetwo', function(req, res) {
    res.sendFile('/page2.html');
});

app.listen('8000', function() {
    console.log("Server is running on port 8000.")
})

客戶端.js

$(function main() {
    $('#page-one-b').click(function() {
        window.location.href = '/pageone'
    }); 

    $('#page-two-b').click(function() {
        window.location.href = '/pagetwo'
    }); 
})

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home</title>
</head>
<body>
    <h1>Welcome home!</h1>
    <p>This is the home page</p>
    <button id="page-one-b">Page 1 button</button>
    <button id="page-two-b">Page 2 button</button>
</body>
<script src="https://code.jquery.com/jquery-2.2.1.min.js"
            integrity="sha256-gvQgAFzTH6trSrAWoH1iPo9Xc96QxSZ3feW6kem+O00="
            crossorigin="anonymous"></script>
<script type="text/javascript" src="client.js"></script>
</html>

page1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Page 1</title>
</head>
<body>
    <p>Page 1 text</p>
</body>
<script src="https://code.jquery.com/jquery-2.2.1.min.js"
            integrity="sha256-gvQgAFzTH6trSrAWoH1iPo9Xc96QxSZ3feW6kem+O00="
            crossorigin="anonymous"></script>
<script type="text/javascript" src="client.js"></script>
</html>

page2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Page 2</title>
</head>
<body>
    <p>Page 2 text</p>
</body>
<script src="https://code.jquery.com/jquery-2.2.1.min.js"
            integrity="sha256-gvQgAFzTH6trSrAWoH1iPo9Xc96QxSZ3feW6kem+O00="
            crossorigin="anonymous"></script>
<script type="text/javascript" src="client.js"></script>
</html>

sendFile 用於發送文件,它不適用於 static 頁。

嘗試

const express = require('express')
const app = express()

app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});

app.get('/pageone', function(req, res) {
res.sendFile(__dirname + '/public/page1.html');
});

app.get('/pagetwo', function(req, res) {
res.sendFile(__dirname + '/public/page2.html');
});

app.listen('8000', function() {
console.log("Server is running on port 8000.")
})

暫無
暫無

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

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