2016-09-18 5 views
-1

私のindex.jsファイルは次のようになります。最大の公共負荷に休憩のエンドポイントがノードサーバーで機能しないのはなぜですか?

var http = require('http'); 
var express = require('express'); 
var path = require('path'); 
var bodyParser = require('body-parser') 

var app = express(); 
var currentVideo = 'https://www.youtube.com/embed/9Q-ovNuireY'; 

// parse application/json 
// parse application/x-www-form-urlencoded 
app.use(bodyParser.urlencoded({ extended: false })) 
app.use(bodyParser.json()) 
app.use(express.static(path.join(__dirname, 'public'))); 

app.get('/', function(res) { 
    console.log("sup"); 
}); 

app.get('/currentVideo', function(req, res) { 
    res.send(200, {youtubeSrc: currentVideo}); 
}); 

http.createServer(app).listen(process.env.PORT ||3000, function() { 
    console.log('Example app listening on port 3000!'); 
}); 

私の静的なページと私のサーバーは罰金起動し、例のアプリのログを出力します。エンドポイントを取得しようとすると動作しませんが、私はhttp://localhost:{port}/currentVideoを打って、404を取得しました。

+0

http:// localhost:{port}/currentVideo'は正しく{{port}が置き換えられましたか?どんなHTTPクライアントでテストしましたか? –

+0

そうですが、ポートは動的ですがその右側です。そして私はブラウザでそれをテストしました。 – Grammin

+0

@Grammin公開ページからAPIへのjavascript呼び出しをここに投稿してください。 –

答えて

0

私はあなたのコードをコピーし、curlを使ってローカルでテストしました。リクエストは正常に機能しました。

あなたの公開ページを共有できますか?データを読み込もうとすると、Chromeデベロッパーツールコンソールにログが表示されますか?

curl -v http://localhost:3000/currentVideo 
* Trying 127.0.0.1... 
* Connected to localhost (127.0.0.1) port 3000 (#0) 
> GET /currentVideo HTTP/1.1 
> Host: localhost:3000 
> User-Agent: curl/7.47.0 
> Accept: */* 
> 
< HTTP/1.1 200 OK 
< X-Powered-By: Express 
< Content-Type: application/json; charset=utf-8 
< Content-Length: 58 
< ETag: W/"3a-FMsS5hMXa1tcIFnGeYHinQ" 
< Date: Sun, 18 Sep 2016 20:34:04 GMT 
< Connection: keep-alive 
< 
* Connection #0 to host localhost left intact 
{"youtubeSrc":"https://www.youtube.com/embed/9Q-ovNuireY"} 
+0

これは実際の回答よりもはるかに多くのコメントです。 –

+0

申し訳ありません、私はStackOverflowで新しく、私はただ助けようとしています。 –

+0

ブラウザのログはきれいに見えますが、エンドポイントは私に404を与えます。私のHTMLファイルはhttp:// localhost:63342/last/app/public/index.htmlのように読み込まれます。私のpackage.jsonには、「main」:「app/index.js」のようなものがありますが、それが重要かどうか分かりません。 – Grammin

関連する問題