node js GET 404 (Not Found)
Your express is never told what to serve when file.js is requested.
app.use('/', index);
app.use('/users', users);
app.use('/firstpage', firstpage);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
Because it's not ., /users, or /firstpage it defaults to the 404 error and returns that. And because it's a js file, you need to use express's static middleware function.
app.use(express.static('routes');
Should be what you want, I would place it after
app.use('/', index);
app.use('/users', users);
app.use('/firstpage', firstpage);
Such that:
app.use('/', index);
app.use('/users', users);
app.use('/firstpage', firstpage);
app.use(express.static('routes');
Beware, this will allow anything in the routes folder to be accessed statically, so you're going to either want to move all your static resources somewhere else, or block off anything in the routes folder you don't want accessed this way.
usersam
Updated on December 12, 2020Comments
-
usersam over 3 years
I am new to node.js. i am running nodejs from bin/wwww and getting "jquery.min.js:4 GET http://127.0.0.1:3000/file.js 404 (Not Found)" error. I am trying very basic and simple thing in a simple web page. Just trying to execute a file on server through ajax call but it always shows file not found error.
In my html page (firstpage.html) i am trying which i want as client request.
<!DOCTYPE html> <html> <head> <meta content="en-us" http-equiv="Content-Language"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <link rel="stylesheet" type="text/css" href="stylesheets/myButton.css"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> var dataString; $(document).ready(function(){ $.ajax({ type: "GET", url: 'file.js', data: dataString, success: function(data){ console.log(data); } }); }); </script> </head> <body> <p class="auto-style1"><strong>Welcome to MyPage</strong></p> <div > <input class="myButton" type="button" onclick="readTextFile(test.txt)" value="Submit" /> </div> <button id="bb">QUERY</button> </body> </html>
and i have a file (file.js) at server ,
var fs = require("fs"); console.log("Going to write into existing file"); fs.writeFile('input.txt', 'Simply Easy Learning!', function(err) { if (err) { return console.error(err); } console.log("Data written successfully!"); console.log("Let's read newly written data"); fs.readFile('input.txt', function (err, data) { if (err) { return console.error(err); } console.log("Asynchronous read: " + data.toString()); }); });
Currently file.js and index.txt are in myapp\public\javascripts location. my app.js is as below,
var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var index = require('./routes/index'); var users = require('./routes/users'); var firstpage = require('./routes/firstpage'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', index); app.use('/users', users); app.use('/firstpage', firstpage); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); // error handler app.use(function(err, req, res, next) { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; // render the error page res.status(err.status || 500); res.render('error'); }); module.exports = app;
Am i making some mistake in configuration ? please help.
-
usersam almost 7 yearsI added "var filejs = require('./routes/file.js');" and "app.use('/file.js', filejs);" and shifted file "file.js" to ./routes but now it is giving error at npm start. throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn)); ^ TypeError: Router.use() requires middleware function but got a Object
-
usersam almost 7 yearsnode start gives error => Going to write into existing file D:\myapp\node_modules\express\lib\router\index.js:458 throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn)); ^
-
usersam almost 7 yearsTypeError: Router.use() requires middleware function but got a Object at Function.use (D:\myapp\node_modules\express\lib\router\index.js:458:13) at EventEmitter.<anonymous> (D:\myapp\node_modules\express\lib\application.js:220:21) at Array.forEach (native) at EventEmitter.use (D:\myapp\node_modules\express\lib\application.js:217:7) at Object.<anonymous> (D:\myapp\app.js:31:5) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3)
-
Kyle Becker almost 7 years@sand Terribly sorry, please look at my updated answer
-
Hannele over 5 yearsIt might also be useful to include some description of what that code does Code snippets can be handy, but it isn't immediately clear to me how it helps answer the original question.