0

I've been doing fine until I try to separate my code into routes, controllers and etc. Now I'm getting an error when I try to load the html file. When I go to the link http://localhost:3000/ I'm getting this error Error: ENOENT: no such file or directory, stat '/views/index.html'

This is my routes.js code

module.exports = function (app) {
    var userController = require('../controllers/userController');

    // app.use(require('express').static('../app/views/index.html'));

    app.get('/', userController.renderHomePage);

    app.post('/find', userController.getUser);
    app.get('/get', userController.getUsers);
    app.post('/add', userController.addUser);
}

And here's my userController.js file

var mongoose = require('mongoose');
var User = require('../models/user');

var express = require('express');

var app = express();
app.use(express.static('../app'));

exports.renderHomePage = function (req, res) {
    res.sendFile('/views/index.html');
}

exports.addUser = function(req,res){
    console.log(req.body);

    var newUser = new User({
        name : req.body.name,
        username : req.body.username,
        password : req.body.password
    });

    newUser.save(function(err){
        if(err){
            console.log(err);
        }
        else{
            console.log("User Saved successfully");
        }
    });

    res.send(req.body);
};

exports.getUsers = function (req, res) {
    // body...
    User.find({}, function(error, users){
        if(error){
            console.log(error);
        }
        else{
            res.send(users);
        }
    })
};

exports.getUser = function (req, res) {
    // body...
    console.log(req.body);
    var data = req.body.username;

    User.find({username : data}, function(err, user){
        if(err){
            throw err
        }
        else{
            console.log(user);
            res.send(user);
        }
    } );
};

Here's my server.js

var express = require('express');
// var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var PORT = process.env.PORT || 3000;

var app = express();

var routes = require('./api/routes/routes');
routes(app);

var database = require('./config/database');

app.use(bodyParser.json());

app.listen(PORT, function(){
    console.log("Server is running on port "+PORT);
})

And here's my folder structure. Here's the folder structure

Server starting without an error. And I thought I've given the paths correctly. But I'm getting the error. Can anyone help me with this ? Thanks.

EDIT : This is how I've linked my script to the html file

<script src="/script/app.js"></script>

5 Answers 5

1

It's been two months, did you solve the problem ?

If not did you try that code :

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

The path you give to the static function is relative to the directory where you run your node process.

In your case, you start your server with/from the server.js file (at the root directory), so the path you give to the static function is relative to this location.

Sign up to request clarification or add additional context in comments.

Comments

1

A path starting with / is an absolute path, meaning it resolves based on the root directory (on Windows, something like C:\, on linux it's just /).

You should be using the path module to get paths to files relative to the module's directory like so:

var path = require('path');
var filePath = path.join(__dirname, 'relative/path/to/file');

__dirname is a special module-scoped variable that provides the path to the current module's containing directory.

1 Comment

I added/changed to this var filePath = path.join(__dirname,'../../app'); exports.renderHomePage = function (req, res) { res.sendFile(path.resolve(${filePath}/views/index.html)); } now my index.html file loads but the scripts are not loading
1
app.use(express.static('../../app'))

Try adding another '..' in your userController.js file, just one .. will put you at the api directory.

Comments

1

Include the 'path' module and change

res.sendFile('/views/index.html');

to

res.sendFile(path.resolve(`${__dirname}/views/index.html`))

5 Comments

I added/changed to this var filePath = path.join(__dirname,'../../app'); exports.renderHomePage = function (req, res) { res.sendFile(path.resolve(${filePath}/views/index.html)); } now my index.html file loads but the scripts are not loading
In your browser are there any console errors such as 404s when trying to pull down your scripts/assets? what are the paths of those assets, and do they exist where the path is pointing at based on your express.static() definition?
Yes. The scripts are in app/scripts/ path. This is the console errors I'm getting. GET http://localhost:3000/script/app.js net::ERR_ABORTED GET http://localhost:3000/script/app.js 404 (Not Found)
You say your scripts are in the 'app/scripts' path, but your error is trying to access the 'script' path (singular, without the last 's'). can you make sure your html page is pointing to the plural 'scripts' path and try again?
sorry. My bad. They are in app/script/ (Screenshot is attached to the question) I've edited the question as the way I've imported the script (app.js) to my html page
1

I ran into this problem. You are sending the html file with res.send, but your scripts are not in a directory that can be reached by your statically available files. Just saw your EDIT. With your EDIT you are closing in on it. Change the reference in your HTML file of your script include.

1 Comment

In the EDIT I've mentioned the way I've referred to my script file in my HTML. I don't see any other way to include it as it's the correct path.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.