jstleger0
jstleger0

Reputation: 105

Access the public folder when using node and Express?

I have looked through stackoverflow and read the express documentation, I can't figure out why the app won't run when I implement "app.use(express.static());" does anyone know a fix?

var express = require('express')();
var app = require('express')();
var server = require('http').Server(app);
var io = require("socket.io").listen(server);

//If i use this my app will not start
// app.use(express.static());

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


//Get input from front-end
io.on('connection', function(socket){
  
	  // On input do something
	  socket.on('directional in', function(unique_id, input, input1){
		  	// send info to index
		    io.emit('directional out', unique_id, input, input1);
	  });
});


server.listen(3000, function(){

	  // Server is running
  	  console.log('listening on *:3000');

});

Any help would be great!

Upvotes: 0

Views: 4242

Answers (1)

Marcos Placona
Marcos Placona

Reputation: 21730

You're not initialising express correctly. The correct way would be as follows:

var express = require('express');
var app = express();

With this, you will be able to do

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

All together, a fully functional express app would look like this in its most basic form:

var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.listen(process.env.PORT || 3000);

Let me know what happens.

Upvotes: 1

Related Questions