MissKnacki
MissKnacki

Reputation: 279

How to fix error Cannot GET with Node.js / Express

I have a class Server : 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;

I start my server in 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'));

My html code : 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>

My index.js :

window.onload = function () {   
    var hist = new Vue({
        el:'#historique',
        methods: {
            openHistorique: function() {
                console.log("On clique");
                document.location.href="../AffichageHistorique/index.php";
            }
        }
    })
}

And the folder structure :

client
  AffichageHistorique
    index.php
  js
    index.js
  index.html
server
  Server.js
app.js

When I click on the button in index.html, I want to open index.php but i have the error Cannot GET /AffichageHistorique/index.php and I don't see how to fix the problem.

Upvotes: 0

Views: 1944

Answers (1)

Quentin
Quentin

Reputation: 944210

Here:

this.app.get('/', function(req, res) {

… you wrote a handler for what happens when the client tries to get /.

You haven't written one for /AffichageHistorique/index.php.

server.app.use('/client', express.static(__dirname + '/client'));

… comes close, but since your URL doesn't start with /client, it doesn't get hit.

Even if it did, it wouldn't execute the PHP.

The static module is for serving static files, not for executing PHP.

If you're using PHP, then consider using a server that PHP is designed to work with (like Apache HTTPD or Lighttpd).

Upvotes: 1

Related Questions