gpt4 book ai didi

node.js - 如何将解析服务器挂载 url 更改为主根 url "/"

转载 作者:搜寻专家 更新时间:2023-10-31 23:52:27 25 4
gpt4 key购买 nike

这是我的 index.js 文件,我在下面的行中进行了更改,因此我可以将解析服务器安装在根 url 中,例如 parse.example.com:1337 而不是 parse.example .com:1337/parse 但我不确定这是否正确,而且我对 nodejs 和 javascript 的经验很少

“var mountPath = process.env.PARSE_MOUNT || '/parse';”到“var mountPath = process.env.PARSE_MOUNT || '/';”

索引.js

var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var path = require('path');

var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;

if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}

var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://parse:secretpass@127.0.0.1:27017/parsedb',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'xxxxxxxxxxxxxxxxxx',
masterKey: process.env.MASTER_KEY || 'xxxxxxxxxxxxxxxx', //Add your master key here. Keep it secret!
serverURL: process.env.SERVER_URL || 'http://localhost:1337/', // Don't forget to change to https if needed
liveQuery: {
classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
}
});

var app = express();

// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));

// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/';
app.use(mountPath, api);

// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
res.status(200).send('I dream of being a website. Please star the parse-server repo on GitHub!');
});

// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function(req, res) {
res.sendFile(path.join(__dirname, '/public/test.html'));
});

var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
console.log('parse-server-example running on port ' + port + '.');
});

// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);

最佳答案

您可以使用 NGINX 作为反向代理。

安装nginx包:

sudo apt-get install -y nginx

打开/etc/nginx/sites-enabled/default

sudo nano /etc/nginx/sites-enabled/default

将其替换为以下内容:

# HTTP - redirect all requests to HTTPS
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;
}

# HTTPS - serve HTML from /usr/share/nginx/html, proxy requests to /parse/
# through to Parse Server
server {
listen 443;
server_name your_domain_name;

root /usr/share/nginx/html;
index index.html index.htm;

ssl on;
# Use certificate and key
ssl_certificate /etc/letsencrypt/live/your_domain_name/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your_domain_name/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

# Pass requests for /parse/ to Parse Server instance at localhost:1337
location /parse/ {
rewrite ^/parse(/.*)$ $1 break;# this is the line that will redirect to root url
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:1337/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
}

location / {
try_files $uri $uri/ =404;
}
}

保存退出并重启

sudo service nginx restart

更新:

  1. 默认情况下,Parse Server 需要 ssl 证书才能工作,您可以禁用它,但强烈建议仅通过 HTTPS 使用它。

  2. 我正在使用 Lets Encrypt 证书,如果您需要帮助创建它们,我可以向您展示我编写的教程,或者您可以使用自己的证书。

关于node.js - 如何将解析服务器挂载 url 更改为主根 url "/",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38910091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com