模块没有导出成员,“请求”

Cro*_*asr 6 javascript express

我看到有很多关于这个主题的帖子,但我没有看到任何与 Express 和我的特定问题相关的帖子,即:

import * as express from 'express';
import { Request, Response, Application } from 'express';  // <-- Error here
Run Code Online (Sandbox Code Playgroud)

给我一个错误(Module has no imported member对于'Request','Response''Application').

我看到子目录中列出了node_modules文件request.jsresponse.js和。根据错误跟踪,我的猜测是 Express 没有检查子目录。使用时如何强制/引导系统签入子目录?(或者这不是问题,而是存在另一个问题?)。application.js/lib/lib/libimport

我尝试过import { Request, Response, Application } from 'express/lib'import * as expressLib from 'express/lib然后import {Request, Response, Application } from expressLib,但这些都不起作用。


注意:我在imports. 。。因为RequestResponse是对象类型,所以我认为它们应该保留大写?

import * as express from 'express';
// import * as expressLib from 'express/lib'
import { Request, Response, Application } from 'express';
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
import * as jwt from 'jsonwebtoken';
import * as fs from "fs";

const app: Application = express();

app.use(bodyParser.json());

app.route('api/login')
    .post(loginRoute);

const RSA_PRIVATE_KEY = fs.readFileSync('/demos/private.key');


export function loginRoute(req: Request, res: Response) {

    const email = req.body.email, 
        password = req.body.password;

        if (validateEmailAndPassword()) {
            const userId = findUserIdForEmail(email);

            const jwtBearerToken = jwt.sign({}, RSA_PRIVATE_KEY, {
                algorithm: 'RS256',
                expiresIn: 120,
                subject: userId
            });
            // res.cookie("SESSIONID", jwtBearerToken, {httpOnly: true, secure: true});

            res.status(300).json({
                idToken: jwtBearerToken,
                expiresIn: ""
            })
        } else {
            res.sendStatus(401);
        }
}

Run Code Online (Sandbox Code Playgroud)

ant*_*nku 5

更新:我没有意识到您使用打字稿,请忽略原始答案。

使用打字稿,如果您安装了@types/express,则大写导入应该可以工作- 因为此包导出大写类型:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express/index.d.ts#L86

可能在重新启动之前,类型包尚未以某种方式建立索引。这是我考虑到问题已解决的唯一猜测。

=========================================

导入模块时 -加载列为主package.json文件的文件。如果没有main文件则从模块根目录加载。package.jsonindex.js

如果是express,则没有main文件并index.js加载./lib/expresshttps://github.com/expressjs/express/blob/master/index.js

module.exports = require('./lib/express');
Run Code Online (Sandbox Code Playgroud)

通过检查./lib/express我们可以看到它导出request,responseapplication以小写形式: https: //github.com/expressjs/express/blob/master/lib/express.js#L59

/**
 * Expose the prototypes.
 */

exports.application = proto;
exports.request = req;
exports.response = res;
Run Code Online (Sandbox Code Playgroud)

因此,为了导入它们,您应该使用小写:

import { response, request, application } from 'express'; 
Run Code Online (Sandbox Code Playgroud)