2017-12-18 9 views
0

シンプルエクスプレスアプリケーションを作成しようとしていますが、コンソールで実行しています。ブラウザでlocalhost:3000を押すと、ネットワークエラーが発生します。私は問題が何であるか分からないようです。シンプルエクスプレスアプリケーションを作成しようとしていますが、動作していないようです。

ここに私のコードです。

var hostname = 'localhost'; 
var port = 3000; 

var app = express(); 

app.use (function (req, res, next) { 
    console.log (req.headers); 

    res.writeHead (200, {'Content-Type': 'text/html'}); 
    res.end ('<html><body><h1>Hello world</h1></body></html>'); 
}); 


// listing for request at port: 7000 no http.createServer needed 

app.listen (console.log (
    `Success server running at http://${hostname}: ${port}` 
)); 

しかし、私が純粋なノードで同様のアプリを作成したとき、うまくいきました。あなたのレビューや応答のための

var fs = require('fs'); 
var path = require ('path'); 
var http = require ('http'); 

var hostname = 'localhost'; 
var port = 3000; 

var server = http.createServer (function (req, res) { 
    console.log ('request for ' + req.url + ' using ' + req.method + ' method'); 


    // checking if the request method is Get 
    if (req.method == 'GET') { 

     var fileUrl; 
     // checking for the request url if it is the home page or not and storing the correct request url in fileUrl variable 
      if (req.url == '/') fileUrl = '/index.html'; 
       else fileUrl = req.url; 
      var filePath = path.resolve ('./public'+fileUrl); 
      var fileExt = path.extname (filePath); 

      if (fileExt == '.html' && req.url !== '/favicon.ico') { 
       fs.exists (filePath, function (exists) { 
        if (!exists) { 
        res.writeHead (404, {'content-type': 'text/html'}); 
        res.end ('<h1> The file </h1>' + fileUrl + '<h1>is not found. Make sure your browser input is correct and try again!</h1>'); 
        console.log('hello no favicon found'); 
        return; 
        } 
       }); 
      } 

     res.writeHead (200, {'content-type': 'text/html'}); 
     fs.createReadStream (filePath).pipe(res); 
    } else { 
     res.writeHead (404, {'content-type': 'text/html'}); 
     res.end ('<h1> The file' + fileUrl + 'not an html file'); 
     console.log (fileUrl); 
    } 
}); 


server.listen (port, hostname, function(){ 
    console.log (`server started ${hostname}:${port}. Helloooooo`); 
}); 

ありがとう:

は、ここに私のコードです!

+1

*エラー*が表示されます。 - >見せて! 'app.listen'の呼び出しは全く意味がありません。' app.listen(port、err => console.log(err)) ' –

+0

あなたのコードをチェックしました。 uは、すべてがうまく動作しているということ以外に、URコードの最初の部分で表現する必要がありませんでした。 – kgangadhar

答えて

0

あなたは行がありません。

var express = require('express') 

あなたのコードの上にこの行を追加して、試してみて、NPMモジュールは

1

問題のカップルがあなたのコードであり、表現追加することを忘れないでください。

expressをプロジェクトにインストールする必要があります。あなたはそうするために以下のコマンドを使うことができます。

npm install express --save 

次に、上のコードを使用してください。

var express = require('express'); 
var app = express(); 

次に、listenコールでポートパラメータを指定する必要があります。

app.listen(3000); 

その他のAPIとオプションはhereです。

0

私はあなたが速く、速く、簡単に、あなたは基本的な作業構造を取得するアプリジェネレータを使用することをお勧めします。

http://expressjs.com/en/starter/generator.html

Expressアプリケーションジェネレータ 使用アプリケーション生成ツール、すぐにアプリケーションのスケルトンを作成するには、発電機を発現します。 express-generatorパッケージは、エクスプレスコマンドラインツールをインストールします。これを行うには、次のコマンドを使用します。-hオプションを使用して

$ npm install express-generator -g 

表示コマンドオプション:

$ express -h 

Usage: express [options] [dir] 
Options: 
-h, --help   output usage information 
     --version  output the version number 
    -e, --ejs   add ejs engine support 
     --hbs   add handlebars engine support 
     --pug   add pug engine support 
    -H, --hogan   add hogan.js engine support 
    -v, --view <engine> add view <engine> support (ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade) 
    -c, --css <engine> add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css) 
     --git   add .gitignore 
    -f, --force   force on non-empty directory 

は、例えば、以下はmyappという名前のExpressのアプリを作成します。、実行のMacOSやLinuxでは

$ cd myapp 
$ npm install 

:アプリは現在の作業ディレクトリにmyappという名前のフォルダに作成され、ビュー・エンジンはパグに設定されます。

$ express --view=pug myapp 

create : myapp 
    create : myapp/package.json 
    create : myapp/app.js 
    create : myapp/public 
    create : myapp/public/javascripts 
    create : myapp/public/images 
    create : myapp/routes 
    create : myapp/routes/index.js 
    create : myapp/routes/users.js 
    create : myapp/public/stylesheets 
    create : myapp/public/stylesheets/style.css 
    create : myapp/views 
    create : myapp/views/index.pug 
    create : myapp/views/layout.pug 
    create : myapp/views/error.pug 
    create : myapp/bin 
    create : myapp/bin/www 

そして、依存関係をインストールこのコマンドを使用して、アプリ:

> set DEBUG=myapp:* & npm start 
01:Windowsでは

$ DEBUG=myapp:* npm start 

は、このコマンドを使用します

ブラウザにhttp://localhost:3000/をロードしてアプリにアクセスします。 生成されたアプリのディレクトリ構造は次のとおりです。

. 
├── app.js 
├── bin 
│ └── www 
├── package.json 
├── public 
│ ├── images 
│ ├── javascripts 
│ └── stylesheets 
│  └── style.css 
├── routes 
│ ├── index.js 
│ └── users.js 
└── views 
    ├── error.pug 
    ├── index.pug 
    └── layout.pug 
7 directories, 9 files 
The app structure created by the generator is just one of many ways to structure Express apps. Feel free to use this structure or modify it to best suit your needs. 
関連する問題