2016-05-14 15 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

あなたはちょうど./public/pages/

に地元の花にapp.js

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

index.htmlページに静的なフォルダに言及し、この

(app.js)を試すことができます

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

これを試してみてください。

関連する問題