今天,我偶然发现了一个内部软件解决方案的大问题,而我之前从来没有想过要在这里开张罚单,但是即使在咨询了另一位同事之后,我们仍然无法弄清楚到底是什么。错误。

我想这将是关于Nginx,pm2或Nodejs缓存的一个很好的教训,对于使用这些Applications / Framework开发的任何人来说,这可能都很有趣。

事实:

设定


Ubuntu 16.04服务器
从Windows 10计算机通过pm2部署到服务器
该应用程序是用nodejs v8.9.4编写的


实际错误之前的故事


首次部署后,该应用程序工作了大约一个半月,没有任何问题
今天,应用程序显示错误:TypeError: Path must be a String. Received undefined
为了使程序的当前状态进入计算机,我继续使用pm2(s)更新功能来更新应用程序。
更新后,该应用程序再次工作。在部署npm install时,再次执行安装,我认为它只是解决了该问题。


实际误差


通过进一步测试,我发现下载的Zip文件的功能之一不再起作用。
在尝试/测试(使用mocha)本地计算机上的相同路由后,我得出以下解决方案,即保存和从中检索文件的位置的路径出了问题。
进行测试以获取应用程序的进一步日志记录无效。
进入路线的网页时,Nginx,pm2或应用程序似乎在我进行编辑之前提供了旧页面。即使服务器上的代码绝对与我计算机上的代码相同。
Cognito也不显示/使用新页面。
正如您在示例中看到的那样,在多次部署要保存的zip文件的其他路径名之后,我得到了这个事实的真正保证。他总是试图用以下路径查找文件:'/web/l18n/archive.zip'
即使我在Route文件中提供了其他路径,该zip文件也始终保存在“ /web/archive.zip”中


代码

别忘了我已经使用很多代码来解决一些明显的问题。如果将请求设置路径设置为archive.zip的保存路径,则这两个部分都不会对Error Message产生任何影响



var zip = new JSZip()

	const dictionaryName = process.env.dictionaryNames
	const splitDictionaryName = dictionaryName.split(';')

	for (const dictionary of splitDictionaryName) {
		const tempDictionaryPath = path.join(__dirname, '/../../' + process.env.dictionaryFolder + '/dictionary.' + dictionary + '.json')
		const dictionaryContent = fs.readFileSync(tempDictionaryPath, 'utf8')
		zip.file('dictionary.' + dictionary + '.json', dictionaryContent)
	}
	await zip.generateAsync({
		type: 'nodebuffer'
	})
		.then(async function (content) {
			const stateString = process.env.state | 'local'
			let archivePath = ''

			if (stateString === 'staging') {
				archivePath = '/web/l18n/current/afasfsagf.zip'
			} else {
				archivePath = path.join(__dirname, '/../aasdasfgasfgs.zip')
			}

			await fs.writeFile(archivePath, content, function (err) {
				if (err) {
					global.log.info(err)
					res.send({})
				} else {
					res.download(archivePath)
				}
			})
		}).catch(function (err) {
			global.log.err(err)
		})





路由

路由是通过上面提供的文件直接加载的。其他所有方法,甚至更高级的文件处理方法都可以正常工作。



const downloadAll = require('./routes/downloadAll.js')

this.http = express()

this.http.get('/download_all', downloadAll)





车把“请求”

正在访问的是通过新窗口的路线,当在主本地路线上按下按钮时会打开。



$('button').click(function (event) {
		let buttonId = $(this).attr('id')
		if (buttonId === 'downloadAllDictionaries') {
			var win = window.open('/download_all')
			window.location.reload()
		}
	})





Nginx的

server {
listen 80;


server_name ****;
access_log /var/log/nginx/****.access.log;
error_log /var/log/nginx/****.error.log;
# pass the request to the node.js server with the correct headers
# and much more can be added, see nginx config options


location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_pass http://127.0.0.1:7000;
    proxy_set_header Host **********;
    proxy_redirect off;
}

listen 443 ssl; # managed by Certbot

include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


if ($scheme != "https") {
    return 301 https://$host$request_uri;
} # managed by Certbot
}

最佳答案

确保nginx不缓存代理请求。

将这两行添加到代理位置:

location / {add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';expires off;}

关于node.js - Pm2,Nginx或Node Js不提供已部署路由的最新版本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52703655/

10-09 18:14
查看更多