私はhttpdispatcherを使用しており、静止画像を提供したいと考えています。私は、/フォルダーをアプリケーションフォルダに持っています。以下のコードがあります。私は画像を取得するためにhttp://localhost:3000/resources/abc.jpgを試しましたが、何の応答も得られませんでした。何か案が?Nodejsはhttpdispatcherで静止画像を配信します
var http = require('http');
var dispatcher = require('httpdispatcher');
var express = require('express');
var app = express();
dispatcher.setStatic('resources');
dispatcher.setStaticDirname('.');
const PORT=3000;
function handleRequest(request, response){
try {
console.log(request.url);
dispatcher.dispatch(request, response);
} catch(err) {
console.log(err);
}
}
dispatcher.onGet("/page1", function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var response={"res":'Page One id: ' + req.params.id};
res.end(JSON.stringify(response));
});
dispatcher.onPost("/post1", function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var response={"res":'Got Post Data appId: '+JSON.parse(req.body).appId};
res.end(JSON.stringify(response));
});
dispatcher.onError(function(req, res) {
res.writeHead(404);
res.end("NOT FOUND");
});
var server = http.createServer(handleRequest);
server.listen(PORT, function(){
console.log("Server listening on: http://localhost:%s", PORT);
});
サーバーを作成しましたか?ディスパッチャを設定することはできますが、 'http.createServer'と' listen'を使ってサーバを作成し、それをポートにバインドする必要があります。 [README'](https://github.com/alberto-bottarini/httpdispatcher/blob/master/README.md)を参照してください(最後に) –
Yepp私はサーバーを作成し、取得リクエストと投稿リクエストから応答を得ました画像リクエストからの応答を得られない@BrandonAnzaldi – nikinci
@nikinciサーバに経路を表示してもよろしいですか? –