[英]How to fix error Cannot GET with Node.js / Express
我有一个 class 服务器:Server.js
const express = require('express');
class Server {
constructor() {
this.app = express();
this.app.get('/', function(req, res) {
res.send('Hello World');
})
}
start() {
this.app.listen(8080, function() {
console.log('MPS application is listening on port 8080 !')
});
}
}
module.exports = Server;
我在 app.js 中启动我的服务器:
const Server = require('./server/Server');
const express = require('express');
const server = new Server();
server.start();
server.app.use('/client', express.static(__dirname + '/client'));
我的 html 代码:index.html
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<title></title>
</head>
<body>
<div id="historique">
<button v-on:click="openHistorique">
Historique Proface
</button>
</div>
</body>
</html>
我的 index.js:
window.onload = function () {
var hist = new Vue({
el:'#historique',
methods: {
openHistorique: function() {
console.log("On clique");
document.location.href="../AffichageHistorique/index.php";
}
}
})
}
和文件夹结构:
client
AffichageHistorique
index.php
js
index.js
index.html
server
Server.js
app.js
当我单击 index.html 中的按钮时,我想打开 index.php 但出现错误Cannot GET /AffichageHistorique/index.php
并查看如何解决问题。
这里:
this.app.get('/', function(req, res) {
…您为客户端尝试获取/
时发生的情况编写了一个处理程序。
您还没有为/AffichageHistorique/index.php
写一篇文章。
server.app.use('/client', express.static(__dirname + '/client'));
…接近了,但是由于您的 URL 不是以/client
开头,因此不会受到打击。
即使这样做,它也不会执行PHP。
static 模块用于提供static文件,而不是用于执行 PHP。
如果您使用的是 PHP,请考虑使用 PHP 设计用于使用的服务器(如 Apache HTTPD 或 Lighttpd)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.