CSS文件在NODE js中不起作用

时间:2016-06-20 20:33:38

标签: javascript html css node.js

出于某种原因,我已将我的css文件附加到我的html文件中。然后我在节点js中使用express打开html文件。但是,当我通过节点js运行Web服务器时,css文件无法打开。我认为既然css文件包含在html中它应该运行??

HTML

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
</head>
  <body>
    <h1>Reading in Value</h1>
    <form action="/" method="post" >
    <br/>
    <label>Enter a UDP command in hex</label>
    <br/><br/>
    <input type="number" name="number" id="number">
    <br/><br/>
    <input type="submit" value="Submit" name="submit">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    </form>
  </body>
</html>

节点js

//Sending UDP message to TFTP server
//dgram modeule to create UDP socket
var express= require('express')
var fs= require('fs')

var util = require('util')
var dgram= require('dgram')
var client= dgram.createSocket('udp4')
var bodyParser = require('body-parser')
var app = express()
var app2= express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())

//Reading in the html gile
app.get('/', function(req, res){
    var html = fs.readFileSync('index2.html');
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(html);
});
//Sends user command utp
app.post('/', function(req, res){
//Define the host and port values
var HOST= '192.168.0.172';
var PORT= 69;
//buffer with hex commands
var message = new Buffer(req.body.number, 'hex');
//Sends packets to TFTP
client.send(message, 0, message.length, PORT, HOST, function (err, bytes) {

        if (err) {
            throw err;
        }
        res.send('UDP message sent to ' + HOST +':'+ PORT);

    });
});

//CREATES ANOTHER PORT
app2.get('/', function(req, res){
 client.on('message', function (message) {
  res.send('received a message: ' + message);
  });
});


app.listen(3000, "192.168.0.136");
app2.listen(8000, "192.168.0.136");
console.log('Listening at 192.168.0.172:3000 and Recieve message will be on 192.168.0.172:8000')

2 个答案:

答案 0 :(得分:4)

DateTime.parse("Wed, 29 Jun 2016 11:35 PM") # => Wed, 29 Jun 2016 23:35:00 +0000 告诉浏览器在<link rel="stylesheet" type="text/css" href="style.css" media="screen" />询问(使用GET)CSS服务器。

查看您的服务器代码。您已经告诉它如何处理/style.cssGET /等),并且您告诉它该怎么做app.get('/', function(req, res){,但您还没有告诉它该怎么做POST /

Express manual涵盖了此内容。

答案 1 :(得分:1)

无论您从何处提供文件,都需要在明确的配置中进行设置:

app.use(express.static('public'));

如果您将静态文件存储在名为public的文件夹中,则可以使用此功能。有关更多文档,请参阅此链接:http://expressjs.com/en/starter/static-files.html