错误是:
Cannot find module 'php'
Error: Cannot find module 'php'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at new View (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\view.js:50:49)
at EventEmitter.app.render (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\application.js:545:12)
at ServerResponse.res.render (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\response.js:938:7)
at F:\Users\MichaelJacksonIsDead\publisherServer\routes\index.js:12:6
at Layer.handle [as handle_request] (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\router\layer.js:82:5)
at next (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\router\route.js:110:13)
此页面呈现为php的路径如下:
router.get('/index.php', function(req, res, next) {
var render = require('php-node')({bin:"F:\\xampp\php\\php.exe"});
res.render('index.php');
});
我在项目级别和级别上运行了npm install -g,npm install node-php -g但没有成功:
F:\Users\MichaelJacksonIsDead\publisherServer>npm install -g
publisherServer@0.0.0 F:\Users\MichaelJacksonIsDead\AppData\Roaming\npm\node_mod
ules\publisherServer
├── php-node@0.0.2
├── debug@2.2.0 (ms@0.7.1)
├── serve-favicon@2.2.1 (ms@0.7.1, fresh@0.2.4, parseurl@1.3.0, etag@1.6.0)
├── cookie-parser@1.3.5 (cookie@0.1.3, cookie-signature@1.0.6)
├── morgan@1.5.3 (basic-auth@1.0.1, depd@1.0.1, on-finished@2.2.1)
├── body-parser@1.12.4 (bytes@1.0.0, content-type@1.0.1, depd@1.0.1, raw-body@2.
0.2, on-finished@2.2.1, qs@2.4.2, iconv-lite@0.4.8, type-is@1.6.2)
├── express@4.12.4 (merge-descriptors@1.0.0, utils-merge@1.0.0, methods@1.1.1, f
resh@0.2.4, cookie@0.1.2, escape-html@1.0.1, cookie-signature@1.0.6, range-parse
r@1.0.2, content-type@1.0.1, parseurl@1.3.0, finalhandler@0.3.6, vary@1.0.0, ser
ve-static@1.9.3, content-disposition@0.5.0, path-to-regexp@0.1.3, depd@1.0.1, on
-finished@2.2.1, qs@2.4.2, etag@1.6.0, proxy-addr@1.0.8, send@0.12.3, type-is@1.
6.2, accepts@1.2.7)
├── hjs@0.0.6 (hogan.js@3.0.2)
└── less-middleware@1.0.4 (mkdirp@0.3.5, node.extend@1.0.10, less@1.7.5)
F:\Users\MichaelJacksonIsDead\publisherServer>npm start
> publisherServer@0.0.0 start F:\Users\MichaelJacksonIsDead\publisherServer
> node ./bin/www
GET /index.php 500 41.145 ms - 954
有没有人对这个问题有任何过期?据我所知,我已正确安装它...也是为了确保我将包添加到我的json文件中,如下所示:
"dependencies": {
"body-parser": "~1.12.4",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.12.4",
"hjs": "~0.0.6",
"less-middleware": "1.0.x",
"morgan": "~1.5.3",
"serve-favicon": "~2.2.1",
"php-node": "0.0.2"
}
最后,我没有足够的代表为项目@ https://www.npmjs.com/package/php-node打开一个标签php-node,这不是一个问题,但是如果有一个更具特色的标签会更好:)
答案 0 :(得分:2)
我找到了答案,谢谢@mscdex和@adeneo
我替换了默认值:
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
与
// use PHP as view engine in Express
var phpnode = require('php-node')({bin:"F:\\xampp\\php\\php.exe"});
app.engine('php', phpnode);
app.set('view engine', 'php');
路线下的:
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index.hjs', { title: 'Express' });
});
router.get('/index.php', function(req, res, next) {
res.render('index.php');
});
该应用程序仍支持php和index.hjs页面。
答案 1 :(得分:1)
当您从npm安装包含命令行实用程序的模块时,仅使用-g
(例如express-generator
就是此示例)。但是,在几乎所有情况下,您只需执行npm install <module name>
即可在本地安装模块,并允许您require()
。
在项目的根目录中npm install
也是如此,require()
应该能够找到php-node
。
答案 2 :(得分:0)
只有当插件被使用时,你才不是 100% 满意,这里有一个简单的方法(这只是一个原型,所以如果你是 node.js 专家,你可以改进它)从仅一个服务器应用程序节点构建。 JS - PHP(理论上你还没有 sqlite3,在安装了 mariadb 或 mysql 服务器后,你也可以使用它们): https://stackoverflow.com/a/68422021/5781320