2016-06-20 7 views
0

何らかの理由で、自分のCSSファイルを自分のhtmlファイルに添付しました。そして、ノードjsでexpressを使ってhtmlファイルを開きます。しかし、ノードjsを介してWebサーバーを実行すると、cssファイルは開かれません。私はCSSファイルがhtmlに含まれているので、それは実行する必要があると思った?CSSファイルがNODEで動作していませんjs

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') 

答えて

4

<link rel="stylesheet" type="text/css" href="style.css" media="screen" />/style.cssでCSSのために(GETで)サーバーを依頼するようにブラウザに指示します。

サーバーコードを確認してください。 GET /app.get('/', function(req, res){など)と何をするべきかを教えて、POST /のために何をすべきかを教えてきましたが、GET /style.cssのために何をすべきかを教えていません。

Express manualがこれをカバーします。

+1

TL:DRでは、express.staticからcss、jsなどのサーバ静的ファイルを使用しています。 – shash7

1

あなたからあなたのファイルを提供しているところはどこでも、あなたがこのような急行設定で設定する必要があります。

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

あなたは静的ファイルと呼ばれるフォルダに格納されていたならこれが働くだろうpublichttp://expressjs.com/en/starter/static-files.html

関連する問題