Nodejs+socket.io with index.php instead of index.html



我有一个简单的使用nodejs的应用程序。我的视图文件是php而不是html。使用 编译 php 文件存在问题:res.sendfile(__dirname + '/index.php'(;它适用于html文件,但不适用于php文件。请帮助如何将 php 与 nodejs 一起使用。

PHP-Express是一个不错的模块,它将帮助您实现自己的要求。它与 Express 版本 3 和 4 兼容。

希望这有帮助!

你可以通过使用 PHP-Express 来实现这一点应用结构

├── app.js
├── package.json
└── views
    └── file.php

并在应用程序中.js

var path = require('path');
var express = require('express');
var app = express();
var router = express.Router();
var phpExpress = require('php-express')({
    binPath: 'php'
});
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 5000));
app.use('/', express.static(__dirname));
app.set('views', path.join(__dirname, '/views'));
app.engine('php', phpExpress.engine);
app.set('view engine', 'php');
app.all(/.+.php$/, phpExpress.router);
app.use(function (req, res, next) {
    res.status(404).send("Sorry can't find that!")
});
app.listen(app.get('port'), function() {
    console.log('Node app is running on port', app.get('port'));
});

你不需要渲染php,视图引擎phpExpress会处理它.router会找到合适的路径并从你的视图文件夹中提供php。

最新更新