Express.js应用入口点 [英] Express.js app entry point

查看:112
本文介绍了Express.js应用入口点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在 index.js 之前运行了一些路由中间件,这使我的应用程序崩溃,因为执行顺序阻止了我按时加载dotenv文件.

It appears some route middleware is running before index.js, which is crashing my application because the order of execution prevents me from loading my dotenv file on time.

我想在所有内容之前加载我的dotenv文件,以确保需要该文件的所有模块都可以访问它.

I would like to load my dotenv file before everything in order to make sure that all modules that require it will have access to it.

但是,在调试此问题时,我注意到在应用程序入口点顶部的 console.log 仍然没有首先登录.

However, while debugging this issue, I noticed that a console.log at the top of the app entry point still doesn't log first.

文件夹结构:

src
------index.js
middleware
------auth.js
routes
------auth.route.js
------index.js
.env

首先记录 middleware/auth.js 的代码:

import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt'

module.exports = function() {
  console.log('this prints first with undefined', process.env.JWT_SECRET) <-------

auth.route.js

import auth from '../middleware/auth'

const userRouter = express.Router()

userRouter.get('/dashboard', auth().authenticate(), function(req, res) {
  res.send('Authenticated, user id is: ' + req.user.id)
})

index.js 文件:

console.log("this prints second");                        <---------
(...)
import routes from './routes'
import express from 'express'
import auth from './middleware/auth'

require('dotenv').config({ silent: process.env.NODE_ENV === 'production' })

const app = express();
(...)
app.use('/api', routes);

推荐答案

从您问题中的代码片段不清楚问题出在哪里,因为我认为您已经省略了导入 auth.route的地方.js 模块.

It's not clear from the code fragments in your question where the problem, because I think you've omitted the place where you import the auth.route.js module.

以下答案假设您实际上是在导入 middleware/auth 之前在 index.js 文件中导入了 auth.route.js >(您发布的代码片段中并未实际使用).因此,基于此假设,可能正在发生以下情况:

The answer that follows assumes that you actually import auth.route.js in the index.js file, probably before importing middleware/auth (which isn't actually used in the code fragment you posted). So, based on that assumption, here's what's probably happening:

要记住的是,模块中的代码在导入时会执行.起初这不会影响 middleware/auth ,因为它所做的只是声明一个函数.但这确实会影响 auth.route.js -查看该模块的作用:

The thing to keep in mind is that code in a module is executed when it is imported. This doesn't affect middleware/auth at first, since all it does is declare a function. But it does affect auth.route.js - look at what that module does:

它从 middleware/auth 模块导入 auth 函数,该模块调用 userRouter.get()函数.该功能的参数之一是 auth()函数的调用.

It imports the auth function from middleware/auth module, which calls the userRouter.get() function. One of the arguments to that function is a call to the auth() function.

如果您要在代码中导入 auth.route.js (我怀疑是),那么那是 所在的 auth 函数被过早调用.

If you're importing auth.route.js in your code (and I suspect you are), then that's where the auth function is getting called prematurely.

现在,如何解决此问题?这实际上非常简单: auth.route.js 文件应导出一个函数,准备就绪时将调用该函数来初始化路由,如下所示:

Now, how to fix this? It's actually pretty easy: the auth.route.js file should export a function which is called to initialize the routes when you're ready, like this:

auth.route.js

import auth from '../middleware/auth'

const userRouter = express.Router()

export default () => {
  userRouter.get('/dashboard', auth().authenticate(), function(req, res) {
    res.send('Authenticated, user id is: ' + req.user.id)
  })

  return userRouter;
}

当您准备初始化路由时(即,当其他所有设置都完成时).

And when you're ready to initialize the routes (ie. when everything else is set up).

import getRouter from './modules/auth.route.js';

app.use('/api', getRouter());

这篇关于Express.js应用入口点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆