failed to load static files express js

2019-08-04 00:15发布

问题:

new to node development.

var app = require('express')()
  , server = require('http').createServer(app)
  , io = require('socket.io').listen(server);

my content gets as below

server.listen(1337);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');

});

I am trying to get static file as below

server.use("/", app.static(__dirname + '/'));

but it doesnt wonk getting errors.

How to get staTIC files?

回答1:

just read express docs

 var express = require('express'),
    app= express(),
      , server = require('http').createServer(app)
      , io = require('socket.io').listen(server);


app.use("/", express.static(__dirname + '/'));


回答2:

the static function is a method on the express module.

So it should be:

var express = require('express'),
app = express(),
io = require('socket.io').listen(server);

app.use(express.static(__dirname + '/'));