我想让我的角度应用程序seo友好,所以我搜索谷歌并找到了几个解决方案Make your Angular App SEO-Friendly (Angular 4 + Universal)我按照说明操作但是当我用npm run start运行项目时发生了以下错误:
src/server.ts(5,42): error TS2307: Cannot find module '../dist/ngfactory/src/app/app.server.module.ngf
actory'.
我整天搜索寻找解决方案我读了这个问题[AOT] @ngtools/webpack throws "Cannot find module './ngfactory/app/app.module.ngfactory'"但是我的问题并没有解决我的问题我的所有软件包都是最新版本,这是我的 在package.json中构建命令
"prestart": "ng build --prod && ./node_modules/.bin/ngc",
"start": "ts-node src/server.ts",
这是server.ts文件:
import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { platformServer, renderModuleFactory } from '@angular/platform-server'
import { enableProdMode } from '@angular/core'
import { AppServerModuleNgFactory } from '../dist/ngfactory/src/app/app.server.module.ngfactory'
import * as express from 'express';
import { readFileSync } from 'fs';
import { join } from 'path';
const PORT = 4200;
enableProdMode();
const app = express();
let template = readFileSync(join(__dirname, '..', 'dist', 'index.html')).toString();
app.engine('html', (_, options, callback) => {
const opts = { document: template, url: options.req.url };
renderModuleFactory(AppServerModuleNgFactory, opts)
.then(html => callback(null, html));
});
app.set('view engine', 'html');
app.set('views', 'src')
app.get('*.*', express.static(join(__dirname, '..', 'dist')));
app.get('*', (req, res) => {
res.render('index', { req });
});
app.listen(PORT, () => {
console.log(`listening on http://localhost:${PORT}!`);
});
答案 0 :(得分:0)
我不知道它是否会有所帮助,但你可以试着放(./) 在"开始" :" th-node ./",
"prestart": "ng build --prod && ./node_modules/.bin/ngc",
"start": "ts-node ./src/server.ts"