L
L
lexstile2018-09-10 11:33:14
Node.js
lexstile, 2018-09-10 11:33:14

How to include styles in node.js?

There is add.html:

<!DOCTYPE html>
<html lang="ru">
<head>
  <meta charset="utf-8"  />
  <title>Add new user</title>
  <meta http-equiv="X-UA-Compatible"  content="IE=edge"  />
  <meta name="viewport"  content="width=device-width, initial-scale=1" >
  <link rel="stylesheet"  href="/css/style.css" type="text/css" />
</head>
<body>
  <div id="form" class="b_form">
    <form action="/register" method="POST">
      <div class="f_item f_title">Add USER</div>
      <div class="f_item"><input type="text" name="uName" placeholder="Enter your name" value=""></div>
      <div class="f_item"><input type="text" name="uLogin" placeholder="Enter your login" value=""></div>
      <div class="f_item"><input type="submit" value="Submit"></div>
    </form>
  </div>
</body>
</html>

There is app.js:
const express = require("express"),
    path = require("path"),
    bodyParser = require("body-parser");

const app = express();
const urlencodedParser = bodyParser.urlencoded({extended: false});

app.use(express.static(__dirname + '/public'));
 
app.post("/register", urlencodedParser, function (request, response) {
    if(!request.body) return response.sendStatus(400);
    console.log(request.body);
  response.send(`${request.body.uName} - ${request.body.uLogin}`);
});
 
app.get("/", function(request, response){
    response.send("<h1>Главная страница</h1>");
});
app.get('/register', function(req, res) {
    res.sendFile(path.resolve(__dirname, 'register.html'));
});
app.listen(3000);

How to make the style.css file work?
When opening directly: Cannot GET /css/style.css

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-09-10
@lexstile

If the file is in the root, then:

<link rel="stylesheet"  href="css/style.css" type="text/css" />

C
cos_is, 2018-09-10
@cos_is

Put it in /public/css/ ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question