Javascript TypeError:尝试加载站点时res.send不是函数

Javascript TypeError:尝试加载站点时res.send不是函数,javascript,node.js,typescript,webpack,Javascript,Node.js,Typescript,Webpack,我最近将构建过程转换为使用typescript和webpack。现在,在解决了大多数TS错误之后,我终于开始尝试运行我的站点了 加载站点时,我从expressjs静态服务器获得TypeError:res.send不是一个函数。这个代码没有改变,即使在我使用gulp的时候,它也一直是一样的,工作很好,但是由于某种原因,在使用webpack和TS之后,它现在给了我这个错误 服务器确实在端口8080上启动并侦听请求。我用“start”:“node--trace warnings dist/server

我最近将构建过程转换为使用typescript和webpack。现在,在解决了大多数TS错误之后,我终于开始尝试运行我的站点了

加载站点时,我从expressjs静态服务器获得
TypeError:res.send不是一个函数。这个代码没有改变,即使在我使用gulp的时候,它也一直是一样的,工作很好,但是由于某种原因,在使用webpack和TS之后,它现在给了我这个错误

服务器确实在端口8080上启动并侦听请求。我用
“start”:“node--trace warnings dist/server/server.js”,

我不知道是因为我用
any
键入了
res
参数还是因为它是什么

完全错误:

TypeError: res.send is not a function
    at dist/server/api.js:58:9
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/node_modules/express/lib/router/index.js:317:13)
dist(从TS和网页包输出)

  • TS正在输出我的服务器文件夹代码
  • webpack正在通过复制和捆绑输出其余内容

webpack.config.js

const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');

const isProduction = process.env.NODE_ENV === 'production';

const html = () => {
  return new HtmlWebPackPlugin({
    template: path.resolve(__dirname, 'src/client', 'index.html'),
    filename: 'index.html',
    hash: true,
  });
};

const copyAllOtherDistFiles = () => {
  return new CopyPlugin({
    patterns: [
      { from: 'src/client/assets', to: 'lib/assets' },
      { from: 'package.json', to: './' },
      { from: 'ext/ink-3.1.10/js/ink-all.min.js', to: 'lib/js' },
      { from: 'ext/ink-3.1.10/js/autoload.min.js', to: 'lib/js' },
      { from: 'ext/js/jquery-2.2.3.min.js', to: 'lib/js' },
      { from: 'ext/ink-3.1.10/css/ink.min.css', to: 'lib/css/ink.min.css' },
      { from: 'feed.xml', to: './' },
      {
        from: 'src/shared',
        to: './shared',
        globOptions: {
          ignore: ['**/*suppressed.json'],
        },
      },
    ],
  });
};

module.exports = {
  entry: './src/client/index.tsx',
  output: {
    filename: 'scripts/app.[hash].bundle.js',
    publicPath: '/',
    path: path.resolve(__dirname, 'dist'),
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js'],
  },
  devtool: isProduction ? '' : 'inline-source-map',
  devServer: {
    writeToDisk: true,
    port: 8080,
  },
  optimization: {
    minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
    splitChunks: {
      cacheGroups: {
        styles: {
          name: 'styles',
          test: /\.css$/,
          chunks: 'all',
          enforce: true,
        },
      },
    },
  },
  module: {
    rules: [
      {
        test: /\.(js)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.(tsx|ts)?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
          },
        ],
      },
      {
        test: /\.less$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'],
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              hmr: process.env.NODE_ENV === 'development',
            },
          },
          'css-loader',
        ],
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        loader: 'file-loader',
        options: {
          outputPath: 'lib/assets/fonts',
        },
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: ['url-loader'],
      },
    ],
  },
  plugins: isProduction
    ? [
        new CleanWebpackPlugin(),
        new MiniCssExtractPlugin({
          filename: isProduction ? 'lib/css/main.[hash].css' : 'main.css',
        }),
        html(),
        copyAllOtherDistFiles(),
      ]
    : [new CleanWebpackPlugin(), html(), copyAllOtherDistFiles()],
};
./tsconfig.json

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */
    "target": "ES2015",                     /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "es2020",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "lib": ["es6", "dom"],                      /* Specify library files to be included in the compilation. */
        "moduleResolution": "node",
        "allowJs": true,                     /* Allow javascript files to be compiled. */
        "checkJs": true,                     /* Report errors in .js files. */
    "jsx": "react",
    "noImplicitAny": true,
        "sourceMap": false,                   /* Generates corresponding '.map' file. */
    "rootDir": "./",                     /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
        "removeComments": true,              /* Do not emit comments to output. */
    "strict": true,                      /* Enable all strict type-checking options. */
        "noUnusedLocals": true,                /* Report errors on unused locals. */
        "noUnusedParameters": true,            /* Report errors on unused parameters. */
//    "rootDirs": ["."],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    "typeRoots": [
      "node_modules/@types"
    ],                      /* List of folders to include type definitions from. */
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
        "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */
//      "resolveJsonModule": true,
        "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true
    },
    "include": [
        "src"
    ],
    "exclude": [
        "/node_modules",
        "/src/server",
        "/src/client/js/ink-config.js",
        "**/test"
  ]
}
./client/server/tsconfig.js(其中
server.ts
api.ts
live)

/src/server/api.ts
api.js
被输出到dist/server)


@num8er在注释中写道,您需要更正传递给
express.use()
的处理程序的签名。代码失败,因为该函数中的第一个参数是请求对象,而不是响应对象。请求对象没有
send()
方法,因此如果您试图发送响应,则需要添加至少两个参数:

import express from 'express'

const app = express();

app.use((req, res) => {
    res.send()
})
Typescript未捕获此错误的原因是您正在使用
any
类型。要真正进行类型检查,请确保没有将这些参数的类型指定为
any
。取而代之的是:

  • 通过删除
    any
    ,允许Typescript正确推断类型;或
  • 明确指定类型:
最后,确保安装了
@types/express

您可以在这里查看:


替换:
。使用((res:any)=>{
到:
。使用((req,res)=>{
或:
。使用((error,req,res,next)=>{
crap是的,我记得在重构过程中取出了res对象,因为我得到了一个未使用的参数TS错误,该错误表示声明了
req',但它的值从未被读取。
。我可以在它前面加一个前缀u。如果我想保留它,即使我不想绕过noUnusedParameters TS错误。谢谢!!@PositiveGuy有趣的是,它可以即使您使用了第二个参数,我也不能在tsconfig中启用该参数,因为我只将其视为eslint警告。
const compression = require('compression'),
  express = require('express'),
  historyApi = require('connect-history-api-fallback'),
  oneYear = 31536000;

module.exports = express()
  .use(compression())
  .on('error', function (err: string) {
    console.log(err);
  })
  .use(historyApi())
  .use(
    express.static('dist', {
      maxage: oneYear,
    })
  )
  .use((res: any) => {
    res.send('Sorry, Page Not Found');
  });
import express from 'express'

const app = express();

app.use((req, res) => {
    res.send()
})
import express from 'express'

const app = express();

app.use((req: express.Request, res: express.Response) => { 

})