- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我绝对是 Node.js 的新手,我尝试通过这项技术创建一个新项目。我使用express框架,但一开始我遇到了一些麻烦。我已经通过解决方法解决了这个问题,但我对下一步行为有疑问:我的 app.js
var express = require('express')
, routes = require('./routes/index')
, routes = require('./routes/login');
var app = module.exports = express.createServer();
console.log(app.env);
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.use( function(req, res, next) {
if (req.url == '/') {
res.render('index', { title: 'Express!' })
} else {
next();
}
});
//app.get('/', routes.index);
app.get('/login', routes.login);
app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
在//Routes block 中,您可以看到app.use和app.get。如果我尝试使用 app.get 而不是 app.use,我会收到错误“cannot get/”。我尝试将index.html 文件放入我的公共(public)文件夹中。但是fot“/”路线我每次都会得到这个文件,而不是index.js的渲染。
app.get('/login', 路线.login); - 工作正常,但“/”路线有问题。我不想让我的代码处于这种状态,请帮助我理解这种行为。
提前致谢。
最佳答案
喜欢用户 PA。提到的,您的代码从未找到 /
url 的原因是因为您正在重新声明您的路由变量:
var express = require('express')
, routes = require('./routes/index') // first declaration of 'routes'
, routes = require('./routes/login'); // re-declaration of 'routes'
这使得您的代码无法访问您的第一个路由声明(指向/index 的声明),这就是您收到错误“无法获取/”的原因,因为您的路由变量仅指向 ./routes/login。
有几种方法可以修复此问题以清理代码:
<强>1。为不同的路线分配不同的变量:
var express = require('express')
, index = require('./routes/index')
, login = require('./routes/login');
- 0R -
<强>2。将多个函数放入路由文件中:
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
// in your routes/index file
exports.index = function(req, res){
res.render('index', { title: 'Index Page' });
};
exports.login = function(req, res){
res.render('login', { title: 'Login Page' });
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
// in your app.js file
// import your 'routes' functions from your 'routes' file
var express = require('express')
, routes = require('./routes/')
// now use your routes functions like this:
app.get('/', routes.index);
app.get('/login', routes.login);
- 或 -
在大型应用程序中,或者为了更好的代码可维护性,您可能希望将不同的路由函数分解到不同的文件中(而不是将所有路由函数放在一个文件中,如上面的示例),因此使用 Express 默认值以设置为例,他们将其 user
函数和 index
函数放置在路由文件夹中,如下所示:
routes /
user.js
index.js
然后,他们像这样设置应用程序:
var routes = require('./routes');
var user = require('./routes/user');
并像这样调用这些函数:
app.get('/', routes.index); // calls the "index" function inside the routes/index.js
app.get('/users', user.list); // calls the "list" function inside the routes/user.js file
希望这有帮助。
快速提示:app.use()
用于创建中间件,这是一个将在应用程序中的每个请求上调用的函数,使开发人员能够访问请求对象req
和响应对象 res
,以及以某种方式更改或增强应用程序的能力。 “在请求中间执行操作”的能力是一个强大的功能,它在原始示例中为您工作的原因是因为您的 app.use()
中间件在以下情况下被调用:即使您的应用无法找到 /
,/
的请求仍被调用,当您重新声明 routes
变量时,该请求已丢失,您仍在向 /
发出请求,您的 app.use() 能够看到该请求(因为每个请求都会调用中间件,即使是“坏”请求),因此您的中间件仍然看到对 /
的[无效]请求,并按如下方式操作:
// This middleware will get called on every request,
// even on the invalid request for '/'
app.use( function(req, res, next) {
// this line of code will see that the
// request is for "/" and fire
if (req.url == '/') {
// the page now appears to render, because you are forcing the
// res.render code inside of your middleware, which isn't always
// recommended, but it is working for you in this example
// because its the only place in your example that can do anything
// when the '/' request is made
res.render('index', { title: 'Express!' })
} else {
// this is common middleware design pattern, the next()
// function tells the express framework to "move on to the next function"
// in the middleware stack
next();
}
});
关于javascript - app.get 不适用于基本路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26213210/
如果我调用一个应用程序两次或多次,但只有一个实例应该运行(这是所需的),我就会遇到一个问题。 首先一些(可能必要的)背景信息: 使用 MAC OS X El Capitan (10.11.6) 我有一
覆盖文件。覆盖 Apps 脚本文件。 这是不是 创建一个新的 Apps 脚本文件的问题。那对我没有帮助。我需要更新 现有的 Apps 脚本文件。这个问题类似于创建一个新文件,但不是同一个问题。更新的语
我是 Apps 脚本的新手,正在尝试了解使用另一个帐户在一个帐户中运行/触发脚本的基础知识。需要注意的是:我想在访问脚本的用户而不是拥有脚本的用户的情况下运行脚本——以便将运行时间分配给访问的用户。
我是 Apps 脚本的新手,正在尝试了解使用另一个帐户在一个帐户中运行/触发脚本的基础知识。需要注意的是:我想在访问脚本的用户而不是拥有脚本的用户的情况下运行脚本——以便将运行时间分配给访问的用户。
我有一个安卓应用程序。我想为我的应用程序实现 App Indexing。 我已经点击了 Google 开发者链接 https://developers.google.com/app-indexing/
有什么区别: import App from '../components/App'; 和 var App = require('../components/App'); 两者都用于获取组件,但它没有
问题: 我有一个使用 requireJS 的简单演示应用程序。当require.min.js脚本加载时,它尝试加载入口点脚本。但是,问题是,而不是 localhost:8090/js/app.js它尝
我正在构建一个 React Native 应用程序,目前正在尝试通过 Firebase Auth 实现一个身份验证注册系统。我已经按照指南/网站上的文档来设置 Firebase 配置文件。我运行该应用
因此 app.yaml 文件的一部分如下所示(至少在 GAE 教程中): handlers: - url: /.* script: main.app 但是,我也看到它看起来像这样: handler
我是Android App开发的新手。当我尝试创建一个新项目Android Project时,弹出以下消息: Information:Gradle tasks [:app:generateDebugS
我正在编写一个应用程序脚本(用于处理电子邮件、任务和日历事件)并希望将其部署为网络应用程序。 该应用程序将在运行它的用户的上下文中运行。该应用程序将被超过 10k+ 的用户使用,甚至可能更多。 在将其
我需要实现一个用于登录网站的 Google Apps 脚本应用,然后如果该网站上的身份验证过程成功,用户应该会在 google 脚本边栏中收到一条消息。 例如:用户输入他的邮箱和密码,然后他点击登录按
我正在开发一个跨平台应用程序,它将在 Google Play 商店和 App Store 上发布。 Google Play 政策以及 App Store 政策规定,您不能使用其他支付系统购买将在应用程
我的 AppEngine 应用程序在我的台式机上的开发服务器上运行良好,但我无法在 Google 服务器上获取版本以关注我的源代码更新。 这是最有说服力的例证。我的 app.yaml 文件开始于: a
我像这样将所有内容重定向到我的 app.yaml 中的单个文件 - url: /.* script: frontcontroller.application 但我仍然必须使用 robots.txt
我想构建一个基于 App Engine 的网络应用程序,并使用 Google 帐户对用户进行身份验证。我需要来自多个域的用户可以登录。从我读到的内容看来,仅使用 Google Accounts API
我无法将我的域指向我使用 Google App Engine 托管的网站。这是背景……注意区分“google apps”(域托管、电子邮件等)和“google app engine”(网站框架)的概念
是否可以通过 App Engine 上内置的 OpenId 实现单点登录?我一直在尝试集成一个 Marketplace 应用程序,并让用户在来自 Google Apps(管理面板或通用导航)时登录。我
有没有办法从 azure-cli 为 Web 应用或函数应用创建和/或激活 App Insights? 现在浏览文档。 最佳答案 我之前也考虑过你的问题。要创建应用程序洞察力,az resource
我在以 Angular 创建新项目时遇到问题。当我运行 ng new myapp 命令时,我得到以下命令 ng 新问候语 Error: Path "/app/app.module.ts" does n
我是一名优秀的程序员,十分优秀!