0

I created app.js file like below:

const express = require('express');

const app = express();

app.use(express.static('public/styles'));
app.use(express.static('public/views'));
app.use(express.static('public/images'));

module.exports = app;

When I add app.listen(8000); and I start my app by node app.js everything looks good. But I wanted to have server in different file so I created server.js file like below:

const http = require('http');
const app = require('../app');

http.createServer(app).listen(8000);

When I started server by node server.js I am getting error message:

Failed to load resource: the server responded with a status of 404 (Not Found)

My path:

-server
 -bin
  -server.js
 -public
  -images
  -styles
   -style.css
  -views
   -index.html
 -app.js

How it should looks like? What's wrong? Please explain me precisely.

I also tried to use it in server.js but didn't work:

const app = require('../app');

app.listen(8000);
Mobeen Sarwar
  • 514
  • 5
  • 23
Robert Daraż
  • 323
  • 2
  • 19

1 Answers1

1

Change path of your statics. It should be relative to your server.js

const express = require('express');

const app = express();

app.use(express.static('../public/styles'));
app.use(express.static('../public/views'));
app.use(express.static('../public/images'));

module.exports = app;
Robert Daraż
  • 323
  • 2
  • 19
Aviso
  • 695
  • 4
  • 12
  • It was brilliant suggestion! I added it ../ before public and now it works great, thanks a lot. It works with app.listen(8000); in server.js file of course. – Robert Daraż Sep 17 '19 at 09:17