我正在尝试在主 Node.js 项目的子目录中运行 Ghost。它目前托管在 azure 网站上。
就像是:
http://randomurlforpost.azurewebsites.net/blog
我按照此处的说明进行操作: https://github.com/TryGhost/Ghost/wiki/Using-Ghost-as-an-NPM-module
新增使用 Ghost 作为 npm 模块后,我还需要 Nginx 或 Apache 吗?截至目前,我的主站点在 localhost:3000 上运行,Ghost 实例在 localhost:2368 上运行。
我尝试对说明中所述的代码部分进行各种修改,但没有成功。
//app.js, is there a specific place to put this?
var ghost = require('ghost');
ghost().then(function (ghostServer) {
ghostServer.start();
});
//config.js
development: {
url: 'http://localhost:3000/blog',
database: {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '/content/data/ghostdev.db')
},
debug: false
},
server: {
host: '127.0.0.1',
port: '2368'
},
paths: {
contentPath: path.join(__dirname, '/content/'),
}
},
//index.js
ghost().then(function (ghostServer) {
parentApp.use(ghostServer.config.paths.subdir,ghostServer.rootApp);
// Let ghost handle starting our server instance.
ghostServer.start(parentApp);
}).catch(function (err) {
errors.logErrorAndExit(err, err.context, err.help);
});
编辑:我能够使用http-proxy路由流量,但是它会将其路由到 localhost:2368/blog (不存在)任何想法如何防止这种情况发生?
var httpProxy = require('http-proxy');
var blogProxy = httpProxy.createProxyServer();
var ghost = require('ghost');
var path = require('path');
// Route /blog* to Ghost
router.get("/blog*", function(req, res, next){
blogProxy.ws(req, res, { target: 'http://localhost:2368' });
});
最佳答案
我遇到了同样的问题,首先尝试使用 Apache 的 ProxyPass 将 /blog
重定向到 端口 2368
,但发现执行此操作时存在其他问题。
在尝试我的建议之前,您应该撤消使用 httpproxy
所做的任何更改。
对我来说似乎有效的方法是将 index.js
中的代码直接放入 app.js
文件中,而不是已有的代码。您需要添加 Ghost 错误变量并将 parentApp
重命名为您的应用程序的名称,我将其称为 yourAppName
,所以很清楚,但我的只是 app
。所以在 app.js
中你可以输入:
var yourAppName = express();
var ghost = require('ghost');
var ghosterrors = require('ghost/core/server/errors')
ghost().then(function(ghostServer) {
yourAppName.use(ghostServer.config.paths.subdir, ghostServer.rootApp);
ghostServer.start(yourAppName);
}).catch(function(err) {
errors.logErrorAndExit(err, err.context, err.help);
});
您可能已经在 app.js 中声明了 ghost
和 express
变量,因此您无需添加这些行。
博客现在应该可以通过 config.js 中指定的 URL 访问。
关于node.js - 在我的主 Node.js 应用程序的子目录中运行 Ghost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28102948/