面试题答案
一键面试性能优化
- 代码压缩与合并
- 使用工具如
uglify - js
(通过npm install uglify - js
安装)。在Node.js脚本中,可以借助它对JavaScript代码进行压缩。例如:
const UglifyJS = require('uglify - js'); const fs = require('fs'); const code = fs.readFileSync('your - angular - app.js', 'utf8'); const result = UglifyJS.minify(code); if (!result.error) { fs.writeFileSync('your - angular - app.min.js', result.code); }
- 对于CSS,可以使用
cssnano
(npm install cssnano
)。类似地,读取CSS文件,通过cssnano
进行压缩处理并输出压缩后的文件。
- 使用工具如
- Tree - shaking
- 在Node.js构建过程中,确保Webpack配置启用了Tree - shaking。在
webpack.config.js
中,设置mode
为'production'
,Webpack在生产模式下默认启用Tree - shaking,它会自动删除未使用的代码。例如:
module.exports = { mode: 'production', // 其他配置... };
- 在Node.js构建过程中,确保Webpack配置启用了Tree - shaking。在
- 优化图片资源
- 利用
image - webpack - loader
(npm install image - webpack - loader
)。在Webpack配置中,添加如下规则:
module.exports = { module: { rules: [ { test: /\.(png|jpg|gif)$/, use: [ { loader: 'image - webpack - loader', options: { mozjpeg: { progressive: true, quality: 65 }, // optipng.enabled: false will disable optipng optipng: { enabled: false }, pngquant: { quality: [0.65, 0.90], speed: 4 }, gifsicle: { interlaced: false }, // the webp option will enable WEBP webp: { quality: 75 } } } ] } ] } };
- 利用
资源缓存
- HTTP缓存配置
- 在Node.js的HTTP服务器(如使用
express
框架,npm install express
)中,可以设置缓存头。例如:
const express = require('express'); const app = express(); app.use((req, res, next) => { // 设置静态资源缓存1年 if (req.url.match(/\.(js|css|png|jpg|gif)$/)) { res.set('Cache - Control','public, max - age = 31536000'); } next(); }); // 其他服务器配置...
- 在Node.js的HTTP服务器(如使用
- 使用CDN
- 将静态资源部署到CDN(如阿里云OSS、腾讯云COS等)。在Node.js构建脚本中,修改资源路径,使其指向CDN地址。例如,通过
html - webpack - plugin
(npm install html - webpack - plugin
)在生成HTML文件时替换资源路径:
const HtmlWebpackPlugin = require('html - webpack - plugin'); module.exports = { plugins: [ new HtmlWebpackPlugin({ template: 'index.html', minify: { collapseWhitespace: true }, cdn: { js: 'https://your - cdn - url.com/js/', css: 'https://your - cdn - url.com/css/' } }) ] };
- 在HTML模板中,可以这样使用:
<html> <head> <% if (htmlWebpackPlugin.options.cdn && htmlWebpackPlugin.options.cdn.css) { %> <link rel="stylesheet" href="<%= htmlWebpackPlugin.options.cdn.css %>styles.css"> <% } %> </head> <body> <% if (htmlWebpackPlugin.options.cdn && htmlWebpackPlugin.options.cdn.js) { %> <script src="<%= htmlWebpackPlugin.options.cdn.js %>main.js"></script> <% } %> </body> </html>
- 将静态资源部署到CDN(如阿里云OSS、腾讯云COS等)。在Node.js构建脚本中,修改资源路径,使其指向CDN地址。例如,通过
服务器端优化
- 负载均衡
- 可以使用
cluster
模块(Node.js内置)实现简单的负载均衡。例如:
const cluster = require('cluster'); const http = require('http'); const numCPUs = require('os').cpus().length; if (cluster.isMaster) { console.log(`Master ${process.pid} is running`); for (let i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', (worker, code, signal) => { console.log(`worker ${worker.process.pid} died`); cluster.fork(); }); } else { http.createServer((req, res) => { res.writeHead(200); res.end('Hello World\n'); }).listen(8000); console.log(`Worker ${process.pid} started`); }
- 可以使用
- 使用PM2进行进程管理
- 安装
pm2
(npm install pm2 - g
)。使用pm2
启动Node.js应用可以实现自动重启、性能监控等功能。例如,启动应用:
pm2 start your - server.js
- 可以设置PM2开机自启:
pm2 startup
- 安装