简体   繁体   English

浏览器自动下载PHP文件

[英]Browser automatically downloads PHP files

i'm reading the book 'Node.js for PHP Developers'. 我正在读“PHP开发者的Node.js”一书。 I have created a NodeJS Web Server and it receives requests and gives response too. 我创建了一个NodeJS Web服务器,它接收请求并给出响应。 But whenever I access a PHP file to reroute it to a JS file (requirement it is), my browser automatically downloads the PHP file. 但每当我访问PHP文件以将其重新路由到JS文件(要求它)时,我的浏览器会自动下载PHP文件。

Here's my JS code, according to which it also downloads JS files(eg when I access localhost:1337/first.njs) 这是我的JS代码,根据它可以下载JS文件(例如当我访问localhost时:1337 / first.njs)

var http = require('http');
var url = require('url');
var file = require('./first.njs');

http.createServer(function(req, res) {
if(url.parse(req.url).pathname == 'first.php')
    file.serve(req, res);
else
    res.end('The file doesn\'t exist');
}).listen(1337, '127.0.0.1');

console.log('Server is running on 1337');

And here's my PHP file if it matters 如果重要的话,这是我的PHP文件

<?php
  echo "ASD";
?>

I know it feels like a really dumb question, but I can't figure out why that happens. 我知道这感觉就像一个非常愚蠢的问题,但我无法弄清楚为什么会这样。

Browsers tested: Chrome and Firefox. 浏览器测试:Chrome和Firefox。

UPDATE Couldn't figure out the exact problem neither could replicate the problem - but this is my latest code if anyone wants to reroute a requested PHP file to a JS file and serve it(the JS file) 更新无法弄清楚确切的问题也无法复制问题 - 但这是我的最新代码,如果有人想将请求的PHP文件重新路由到JS文件并提供它(JS文件)

var http = require('http');
var url = require('url');
var file = require('./first.njs');

http.createServer(function(req, res) {
if(url.parse(req.url).pathname == '/first.php')
    file.serve(req, res);
else
    res.end('File not found');
}).listen(1337, '127.0.0.1');

console.log('Server is running on 1337');

If you want to check if a PHP page can be rendered without actually rendering it to the page you can use an ajax request in JavaScript/jQuery. 如果要检查是否可以呈现PHP页面而不实际将其呈现到页面,则可以在JavaScript / jQuery中使用ajax请求。

    $.ajax({
        type: "POST",
        url: 'YOUR PHP FILE PATH',
        success: function (dataCheck) {
            // file was accessed
        }
    });

Inside your success function you can output that the PHP file was successfully accessed. 在您的成功函数中,您可以输出已成功访问PHP文件。

Once you get a better handle on node, you'll probably want to use expressjs, take a look at expressjs routing . 一旦你在节点上得到了更好的处理,你可能想要使用expressjs,看看expressjs路由

app.get('/first.php', function(req, res){
    res.send('You accessed first.php!');
});

If you then wanted to display the php, you could use php-node to render php in node.js. 如果你想显示php,可以使用php-node在node.js中呈现php。

It seems that you don't have Apache (or some other server) or PHP configured. 您似乎没有配置Apache(或其他服务器)或PHP。 The request has to go to the PHP script first. 请求必须首先转到PHP脚本。 So you would have to hit localhost:80/file.php and then in the PHP script redirect to localhost:1337/file.js. 所以你必须点击localhost:80 / file.php,然后在PHP脚本中重定向到localhost:1337 / file.js。

A better suggestion for what you want to do is actually reverse proxy from Apache to Node.js and then have Node.js read the path. 对你想要做的更好的建议实际上是从Apache到Node.js的反向代理,然后让Node.js读取路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM