物业'渲染'类型' NextFunction'上不存在。 Angular universal + express

时间:2018-05-17 05:37:39

标签: angular express webpack angular-universal

所以我尝试为Angular 6 app制作SSR并且我得到了这个错误,我使用的是角度cli通用演示作为一个例子:

Property 'render' does not exist on type 'NextFunction'.

这是我的server.ts文件:

import 'zone.js/dist/zone-node';
import 'reflect-metadata';

import { enableProdMode } from '@angular/core';
import {ngExpressEngine} from '@nguniversal/express-engine';
import {provideModuleMap} from '@nguniversal/module-map-ngfactory-loader';
import * as express from 'express';
import { join } from 'path';

import 'localstorage-polyfill';

global['localStorage'] = localStorage;
// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();

// Express server
const app = express();

const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist');

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');

app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));

app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));

// TODO: implement data requests securely
app.get('/api/*', (req, res) => {
  res.status(404).send('data requests are not supported');
});

// Server static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser')));

// All regular routes use the Universal engine
app.get('*', (err, req, res) => {
  console.log(err);
  res.render('index', { req });
});

// Start up the Node server
app.listen(PORT, () => {
  console.log(`Node server listening on http://localhost:${PORT}`);
});

和我可爱的package.json

{
  "name": "xxxxx",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "tslint ./src/**/*.ts -t verbose",
    "e2e": "ng e2e",
    "build:ssr": "npm run build:client-and-server-bundles && npm run webpack:server",
    "serve:ssr": "node dist/server",
    "build:client-and-server-bundles": "ng build --prod --aot --vendor-chunk --common-chunk --build-optimizer --named-chunks && ng run jinni-angular:server",
    "webpack:server": "webpack --config webpack.server.config.js --progress --colors"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.0",
    "@angular/cdk": "github:angular/cdk-builds",
    "@angular/common": "^6.0.0",
    "@angular/compiler": "^6.0.0",
    "@angular/core": "^6.0.0",
    "@angular/forms": "^6.0.0",
    "@angular/http": "^6.0.0",
    "@angular/material": "github:angular/material2-builds",
    "@angular/platform-browser": "^6.0.0",
    "@angular/platform-browser-dynamic": "^6.0.0",
    "@angular/platform-server": "^6.0.1",
    "@angular/pwa": "^0.6.1",
    "@angular/router": "^6.0.0",
    "@angular/upgrade": "^6.0.0",
    "@nguniversal/express-engine": "^6.0.0",
    "@nguniversal/module-map-ngfactory-loader": "^6.0.0",
    "@ngx-translate/core": "^9.1.1",
    "@ngx-translate/http-loader": "^2.0.1",
    "@types/underscore": "^1.8.6",
    "angular-in-memory-web-api": "^0.6.0",
    "classlist.js": "^1.1.20150312",
    "core-js": "^2.5.4",
    "global": "^4.3.2",
    "hammerjs": "^2.0.8",
    "jquery": "latest",
    "localstorage-polyfill": "^1.0.1",
    "ng-custom-select": "^1.0.4",
    "ng-select": "^1.0.0-rc.3",
    "ng2-breadcrumbs": "^0.1.281",
    "ng2-carouselamos": "^3.2.0",
    "ng2-translate": "^5.0.0",
    "ng4-click-outside": "^1.0.1",
    "ng4-intl-phone": "^1.2.0",
    "ngx-device-detector": "^1.2.2",
    "ngx-dropdown": "0.0.22",
    "ngx-owl-carousel": "^2.0.7",
    "node": "^9.11.0",
    "npm": "^5.8.0",
    "rxjs": "^6.0.0",
    "rxjs-compat": "^6.1.0",
    "serve": "^6.5.3",
    "ts-loader": "^4.3.0",
    "underscore": "^1.8.3",
    "webstorage-polyfill": "^1.0.1",
    "zone.js": "^0.8.24"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.1",
    "@angular/cli": "^6.0.0",
    "@angular/compiler-cli": "^6.0.0",
    "@angular/language-service": "^5.0.0",
    "@angular/platform-server": "^6.0.0",
    "@types/express": "^4.11.1",
    "@types/jasmine": "~2.8.0",
    "@types/jasminewd2": "^2.0.3",
    "@types/node": "^6.0.45",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.99.1",
    "express": "^4.15.2",
    "http-server": "^0.10.0",
    "pre-commit": "^1.2.2",
    "reflect-metadata": "^0.1.10",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~1.4.2",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-phantomjs-launcher": "^1.0.2",
    "lodash": "^4.16.2",
    "node-sass": "^4.7.2",
    "phantomjs-prebuilt": "^2.1.7",
    "protractor": "~5.3.0",
    "ts-loader": "^4.2.0",
    "ts-node": "^5.0.1",
    "tslint": "^5.9.1",
    "typescript": "2.7.2",
    "webpack-cli": "^2.0.14"
  },
  "repository": {}
}

我卡住了。我尝试过不同的想法让它发挥作用而没有任何帮助....任何想法的人?

顺便说一句我得到这个错误:webpack --config webpack.server.config.js --progress --colors

我的webpack.server.config.js是:

    const path = require('path');
const webpack = require('webpack');

module.exports = {
  entry: { server: './server.ts' },
  resolve: { extensions: ['.js', '.ts'] },
  target: 'node',
  mode: 'none',
  // this makes sure we include node_modules and other 3rd party libraries
  externals: [/node_modules/],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: '[name].js'
  },
  module: {
    rules: [{ test: /\.ts$/, loader: 'ts-loader' }]
  },
  plugins: [
    // Temporary Fix for issue: https://github.com/angular/angular/issues/11580
    // for 'WARNING Critical dependency: the request of a dependency is an expression'
    new webpack.ContextReplacementPlugin(
      /(.+)?angular(\\|\/)core(.+)?/,
      path.join(__dirname, 'src'), // location of your src
      {} // a map of your routes
    ),
    new webpack.ContextReplacementPlugin(
      /(.+)?express(\\|\/)(.+)?/,
      path.join(__dirname, 'src'),
      {}
    )
  ]
};

1 个答案:

答案 0 :(得分:1)

get方法的回调无效

app.get的回调必须是一个或多个中间件函数(请参阅expres app.get documentation

如果为中间件函数提供3个参数,则第3个参数为nextFunction(Middleware functions' documentation

试试

app.get('*', (req, res) => {

  res.render('index', { req });
});