2017-12-18 95 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

我检查了你的代码。你错过了要求在第一部分的代码除了一切工作正常。 – 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应用程序生成 使用应用程序生成工具,表达发电机,快速创建应用程序框架。 快速生成器包安装快速命令行工具。使用以下命令这样做:

$ npm install express-generator -g 

显示-h选项的命令选项:

$ 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的快速应用。该应用程序将在一个文件夹名为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 

然后安装依赖:

$ cd myapp 
$ npm install 

在MacOS或Linux上运行应用程序使用此命令:

$ DEBUG=myapp:* npm start 

在Windows上,使用这个命令:

> set 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. 
相关问题