-1

i want to get css file via Node.js localhost. index.php load correct, style.css can't be loaded.

my structure files

app.js views
     index.php
     css
        style.css

my app.js

const http = require('http');
const fs = require('fs');

const hostname = '127.0.0.1';
const port = 3000;

fs.readFile('views/index.php', (err, html) => {
    if (err) {
        throw err;
    }

    const server = http.createServer((req, res) => {
        res.statusCode = 200;
        res.setHeader('Content-type', 'text/html');
        res.write(html);
        res.end();
    });

    server.listen(port, hostname, () => {
        console.log('Server started on port ' + port + (Date.now()));
        console.log('address ' + hostname);
    });
});

my index.php

<html>
    <head>
        <link rel="stylesheet" href="css/style.css" />
        <title>title</title>
    </head>
</html>
1
  • Your server is giving the html file to the client on every response, that's why your style can't be loaded. Commented Feb 7, 2018 at 8:10

1 Answer 1

0

If you wanna use php, maybe you should consider something other than js for the server. There are packages like MAMP etc. the reason css is not loaded is you only have one way of reaching your server, and that renders your index file.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.