I have been following the a scotch.io tutorial to create my first node and angular app. I have seen that relative paths are a common issue online for the Error: ENOENT: no such file or directory which as far as I can tell is the same as the tutorial so I'm why its not working.
The full error message is:
Error: ENOENT: no such file or directory, stat'/Users/badman/githubRepos/travelGuide/app/public/index.html'
at Error (native)
My folder structure is here.
My server.js:
// set up web server
var express = require('express');
var app = express();
var bodyParser = require("body-parser");
// routes
require('./app/routes.js')(app);
// listen (start app with node server.js)
app.listen(3000, function() {
console.log("server going");
})
routes.js:
module.exports = function (app) {
app.get('*', function (req, res) {
res.sendFile(__dirname + '/public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});
};
Any help is appreciated :)
I had the same problem and I moved
app.get('*', function (req, res) {
res.sendFile(__dirname + '/public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});
to server.js right under the require('./app/routes.js')(app); and that fixed it! Because it was looking for index.html in the wrong place when a server-side route was being called. I think you could have also changed the path too but I found this to be clearer.
Related
I'm setting up express server with create-react-app.
AT console I'm getting
Uncaught SyntaxError: Unexpected token < bundle.js:1
When I click on error it shows me homepage html.
While using morgan logger its giving me 200 ok.
GET /static/js/bundle.js 304 1.145 ms - -
Also when i view source click on css link it shows me css while click on javascript link it shows me blank page(homepage html).
Here is the the server code.
var express = require('express');
var bodyParser = require('body-parser');
var logger = require('morgan');
var path = require ('path');
var data = {};
express()
.use(logger('dev'))
.use(express.static(path.join(__dirname, 'flashcard-app/build')))
.use(bodyParser.json())
.get('/api/test', (req, res) => res.json("Great news every thing is working fine."))
.get('/api/data', (req, res) => res.json(data))
.post('/api/data', (req, res) => res.json(data = req.body))
.get('*', function(req, res) {
console.log('serving path:', path.join(__dirname+'/flashcard-app/build/index.html'));
res.sendFile(path.join(__dirname, 'flashcard-app/build/index.html'));
})
.listen(3333, function(){
console.log('server running at 3333');
});
Also this is custom server.js file not one that provided by express-generator. I'm running by node server_old.js instead of using ./bin/www
Help comment tips appreciated.
You are trying to fetch /static/js/bundle.js, but there is no bundle.js in static/js.
I think you are trying to use index.html that tries to bundle.js while your webpack config builds main.[hash].js
Introduction
I have built some back end functionality in Node (First time using Node). Problem is that the whole thing was built in one page (index.js) so now im following a few basic tutorials and setting out express router middleware and now trying to follow a modular MVC approach,
This code is simple but brakes when I separate into two pages Server.js and config.js. I know its a simple problem but i cant spot it. can someone help spot the problem and maybe improve the structure ?
Problem
I go to http://localhost:8080/about or a different route and I get
Cannot GET /about
rather than the correct print out.
back-end/server.js
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
// get an instance of router
var router = express.Router();
// START THE SERVER
// ==============================================
app.listen(port);
console.log('Server has started!! ' + port);
back-end/config.js
router.use(function(req, res, next) {
console.log(req.method, req.url);
next();
});
router.get('/', function(req, res) {
res.send('im the home page!');
});
// sample route with a route the way we're used to seeing it
router.get('/sample', function(req, res) {
res.send('this is a sample!');
});
router.get('/about', function(req, res) {
res.send('im the about page!');
});
app.route('/login')
.get(function(req, res) {
res.send('this is the login form');
})
.post(function(req, res) {
console.log('processing'); // shows on console when post is made
res.send('processing the login form!'); // output on postman
});
app.use('/', router);
As #SLaks said in his comment, you need to import (require) your backend/config.js file. But it's not as simple as that...
In node, variables are scoped to the file in which they appear, so if you simply add require('./config') to your server.js file, that's not going to work either, because the router variable in config.js is local to that file - it's not going to know about the router variable in server.js.
The solution to this is to have the config.js file export a function which the server.js file can use to configure stuff. For example
config.js
module.exports = function(router) {
// set up your router here with router.use, etc.
};
server.js
var configure = require('./config');
// after you set up your express router...
configure(router);
// now start listening
Here is my folder structure:
I have everything inside the src folder, src/index.html is what I'm trying to point too. And my node server file is in src/server/server.js
When I run the correct node src/server/server command I get the following error:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.sendFile('index.html', { root: __dirname });
});
app.get('/category', (req, res) => {
res.end('Categories');
});
app.listen(8999, (res) => {
console.log(res);
});
Error: ENOENT: no such file or directory, stat '/Users/leongaban/Projects/CompanyName/appName/src/server/index.html'
So the error message is telling me I need to go 1 more folder up, so figured something like the following:
app.get('/', (req, res) => {
res.sendFile('../index.html', { root: __dirname });
});
However now I get a Forbidden error:
ForbiddenError: Forbidden
The reason you get a Forbidden error is because Node.js finds the relative path ../ malicious (for example if you get a file based on user input he might try to reach files on your file system which you don't want him to access).
so instead you should use the path module like pointed in this question
Your code should use path like so:
var path = require('path');
res.sendFile(path.resolve(__dirname + '../index.html'));
I had a similar problem with a mean stack problem earlier on.
Mor Paz' above answer should work. :)
Just to contribute, i did the following.
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
res.sendFile('../index.html');
});
I have a public folder in which I put an angular2 app. Now I am trying to setup an express server with a catchall route that always returns index.html. To be clear - according to this question I need to map all of my routes to index.html.
If I access the base server URL (localhost:10001), everything works as expected. But when I go to a route(let's say localhost:10001/landing), and refresh the page, I get the following error:
Error: ENOENT: no such file or directory, stat
'/Users/shooshte/express-test/index.html' at Error (native)
This is my server configuration:
var express = require('express');
var static = require('serve-static');
var server = express();
// middleware
server.use(express.static(__dirname + '/public'));
// routes
server.use('*', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
var port = 10001;
server.listen(port, function() {
console.log('server listening on port ' + port);
});
What am I doing wrong?
You're missing the public directory in the path to index.html:
res.sendFile(__dirname + '/public/index.html');
i think you don't have your index.html in either public or your root folder /Users/shooshte/express-test/
this is the only resion you are getting this error
I have started a node js project and up to now I have included my html pages/css/etc in a folder named 'html'. I have npm- installed the relevat modules also. But some error message is displayed during the launch. plz help me out.Thnx
Project Hierarchy
squadra-server.njs
var express = require('express');
var app = express.createServer();
app.get('/', function(req, res){
res.send('Hello World');
});
app.configure(function () {
app.use(express.static(__dirname + '/html'));
})
app.listen(8000);
error msg
You have to configure the path to node.exe in your run configuration. Should not be much of a problem...