简体   繁体   English

节点我的应用程序如何找到index.js

[英]node how can My app find index.js

Folder structure 资料夹结构

  • bin - www.js 斌-www.js

  • lib - jsFiles... lib-jsFiles ...

  • models - jsFiles... 模型-jsFiles ...

  • node_modules -Folders and Files node_modules-文件夹和文件

  • public - index.html 公共-index.html

  • route - jsFiles... 路线-jsFiles ...

  • index.js index.js

  • package.json package.json

I use Express, angular.js. 我使用Express,angular.js。 Server starts at www.js and It calls index.js. 服务器从www.js开始,并调用index.js。 After that, When I type merely "localhost:3000" It shows me public/index.html. 之后,当我仅键入“ localhost:3000”时,它将显示为public / index.html。 I don't have route for '/' but It shows me 'public/index.html'. 我没有“ /”的路线,但显示为“ public / index.html”。 I can not understand this. 我无法理解这。 Please let me know about the process. 请让我知道有关过程。

www.js www.js

var debug = require('debug')('example-server');
var app = require(process.cwd()+'/index');

//listen at 3000 port
app.set('port',process.env.PORT || 3000);


var server = app.listen(app.get('port'),function()
{
    debug('Express server listening on port ' + server.address().port);
});

index.js index.js

var favicon      = require('serve-favicon');
var express      = require('express');
var path         = require('path');
var logger       = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser   = require('body-parser');
//Connection for DB
require('./lib/connection');
var employees    = require('./routes/employees');
var teams        = require('./routes/teams');
var app = express();

// Writing at routing table
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended:true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname,'public')));

app.use(employees);
app.use(teams);


// send 404 to errorhandler
app.use(function(err,req,res,next)
{
    var err = new Error('Not Found');
    err.status = 404;
    console.log(error);
    next(error);
});

...
...
module.exports = app;

将相对路径放置到文件夹(在层次结构中向上)。

var app = require('../index');

In express.js the sequence in which you register your middleware makes a huge difference. 在express.js中,注册中间件的顺序有很大的不同。

When express.js receives a request, it starts from top and executes registered middleware. 当express.js收到请求时,它从顶部开始并执行注册的中间件。

Middlewares are registered in express app using app.use(middleware_goes_here) this type of middleware gets executed no matter what the request url is on the other hand you can also register a middleware like app.use('/url/path',middleware_goes_here) in this case we are registering this middleware to '/url/path' so this middleware will only get executed when you visit '/url/path' (and non of the previous matching middleware serves the request without calling next() ) 中间件是使用app.use(middleware_goes_here)在快速应用程序中注册的,无论请求的URL是什么,这种中间件都会执行,另一方面,您也可以注册像app.use('/url/path',middleware_goes_here)在这种情况下,我们正在将该中间件注册到'/ url / path',因此,仅当您访问'/ url / path'时,该中间件才会执行(并且以前匹配的中间件中的任何一个都将在不调用next()的情况下提供请求)

This app.use(express.static(path.join(__dirname,'public'))); 这个app.use(express.static(path.join(__dirname,'public'))); line of code does the magic. 代码行神奇了。

You can go here (express.static ref) to know more about static content serving and routing. 您可以在此处(express.static ref)了解有关静态内容服务和路由的更多信息。

Basically what happens is, we are configuring express.static middleware to serve static content "as is" from "public" folder. 基本上发生的是,我们正在配置express.static中间件以“按原样”从“ public”文件夹中提供静态内容。 So when you make any request and it matches a static content in public folder, then it will serve it otherwise express.static will call next middleware in sequence. 因此,当您发出任何请求且它与公用文件夹中的静态内容匹配时,它将为它提供服务,否则express.static将按顺序调用下一个中间件。

So in your case, the first middleware that actually server input request is express.static without any route filters, so it servers index.html even without a specifically defined route. 因此,在您的情况下,实际上是服务器输入请求的第一个中间件是express.static没有任何路由过滤器,因此即使没有专门定义的路由,它也可以index.html If your public folder had file at public/javascript/jquery.js then following url will map to it http://localhost:3000/javascript/jquery.js 如果您的public文件夹中的文件有public/javascript/jquery.js那么以下网址将映射到它http://localhost:3000/javascript/jquery.js

NOTE: You do not have to specify "public" in the url, the way in which express.static is registered, it will server contents FROM "public" folder. 注意:您不必在URL中指定“ public”,即express.static的注册方式,它将从“ public”文件夹中存储内容。

................ ................

UPDATE: How does default work in express.static ? 更新:default如何在express.static工作?

By default, app.use(express.static(path.join(__dirname,'public'))); 默认情况下, app.use(express.static(path.join(__dirname,'public'))); this will take index.html as default document. 这将使用index.html作为默认文档。 If you want to set index2.html as your default document, you can do that by doing something like this app.use(express.static(path.join(__dirname,'public'),{index: 'index2.html'})); 如果您想将index2.html设置为默认文档,则可以通过执行以下app.use(express.static(path.join(__dirname,'public'),{index: 'index2.html'}));来做到这一点app.use(express.static(path.join(__dirname,'public'),{index: 'index2.html'}));

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM