Node.js不提供css文件

时间:2018-02-06 08:37:20

标签: css node.js

我正在使用socket.io框架从我自定义的IoT设备获取数据。设备使用TCP / IP协议,我的网站使用HTTP协议。所以,沟通没有问题,但我在网页上看不到任何CSS样式。

我有很多css文件和css文件目的地。

This is expected screen

My file index

这是我的server.js代码:

render(){
        return(
            <Card>
                <CardSection>
                    <Input 
                        borderColor="transparent" //props for border
                        label="Email"
                        placeHolder="abc@example.com"
                        onChangeText={this.onEmailChanged.bind(this)}
                        value={this.props.email}
                    />
                </CardSection>

                <CardSection>
                    <Input 
                        borderColor="transparent" // props for border
                        secureTextEntry
                        label="Password"
                        placeHolder="password"
                        onChangeText={this.onPasswordChanged.bind(this)}
                        value={this.props.password}
                    />
                </CardSection>

                {this.errorRender()}

                <CardSection>
                    {this.spinerRender()}
                </CardSection>
            </Card>
        );
    }

这是我的index.html;

// socket io
var app = require('http').createServer(handler),
    io = require('socket.io').listen(app),
    fs = require('fs'),
    express = require('express'),
    exp = express();

exp.use('*/css',express.static('css'));

app.listen(80, function() {
  console.log('Socket IO Server is listening on port 80');
});

function handler(req, res) {
  fs.readFile(__dirname + '/index.html', function(err, data) {
    if(err) {
      res.writeHead(500);
      return res.end('Error');
    }
    res.writeHead(200);
    res.write(data);
    res.end();
  })
};


io.on('connection', function(socket) {
  console.log('connection...');
  socket.on('emit_from_client', function(data) {
    console.log('socket.io server received : '+data);
    io.emit('emit_from_client', data);
  });
});

// TCP server
var net = require('net');
var writable = require('fs').createWriteStream('test.txt');


net.createServer(function (socket) {
  console.log('socket connected');
  socket.on('data', function(data) {
    var line = data.toString();
    console.log('got "data"', line);
    socket.pipe(writable);
    io.emit('emit_from_client', line); // socket.io
    //socket.emit("OK\r\n"); // socket.io
    socket.write("OK1\r\n");
  });
  socket.on('end', function() {
    console.log('end');
  });
  socket.on('close', function() {
    console.log('close');
  });
  socket.on('error', function(e) {
    console.log('error ', e);
  });
  socket.write('Hello from tcp server\r\n');
}).listen(3080, function() {
  console.log('TCP Server is listening on port 3080');
});

1 个答案:

答案 0 :(得分:0)

确保您的资产为statically available

app.use(express.static('css'))

由于您的资产无法以这种方式访问​​,因此输出中的内容并未反映

stated作为@ArpitSolanki,请尝试:

exp.use('/css/*',express.static('css'));

另外,根据您的代码:

exp.use('*/css',express.static('css'));

我从未见过这样的道路。