2016-05-14 72 views

回答

2

有沒有辦法使用Express提供的一些方便的設備訪問靜態文件目錄?不,我不知道。

有沒有辦法讓你想做的事情?當然。請使用fs模塊閱讀。我也喜歡使用path模塊來生成我的文件路徑。

const path = require("path"); 
const fs = require("fs"); 

// Do however you like to build paths. 
// I like to use resolve so I always get an absolute path. 
const publicPath = path.resolve(__dirname, "public"); 

const htmlPath = path.join(publicPath, "thefile.html"); 

app.post('/', function (req, res, next) { 
    fs.readFile(htmlPath, "utf8", onFile); 

    function onFile (err, html) { 
    if (err) return next(err); // assuming you're using an error handler, like you probably should be 
    mailgunThatStuff(html, mgDone); 
    } 

    function mgDone (err) { 
    if (err) return next(err); 
    res.end("OK mailgun'd that thing"); 
    } 
} 

這可能有點羅嗦。合理?

0

你可以試試這個

(app.js)你剛纔提到的靜態文件夾在本地花app.js

var express = require('express'); 
var app = express(); 
app.use(express.static('public')); 

index.html頁面中./public/pages/

app.all('/', function (req, res) { 
    res.sendFile('index.html', {root: './public/pages/'}); 
}); 

試試這工作正常

相關問題