Reputation: 2308
I have setup new express (nodejs) project and while I tried to run url it shows me an error of 404 not found error. Yet I have not modified any single line of code and directory structure. I have used express-generator command to create express project's structure.
Can anyone provide me suggestions for following issues? - Why default code does not run? - How should I setup my project directory to prevent write this long url. - How could I integrate my chat application with yii1 project.
Following are my code details.
URL : http://localhost:3000/wchat/wnode/users
Directory structure : wchat/wnode/users wchat : php project wnode : node directory to implements instant notification and chat users : module
Code files : app.js
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 routes = require('./routes/index');
var users = require('./routes/users');
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('/', routes);
app.use('/users', users);
// 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 handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
index.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
users.js
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
module.exports = router;
Error :
Not Found
404
Error: Not Found
at app.use.res.render.message (/var/www/html/wchat/wnode/app.js:30:13)
at Layer.handle [as handle_request] (/var/www/html/wchat/wnode/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:312:13)
at /var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:330:12)
at next (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:271:10)
at /var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:618:15
at next (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:256:14)
at Function.handle (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:176:3)
at router (/var/www/html/wchat/wnode/node_modules/express/lib/router/index.js:46:12)
Upvotes: 0
Views: 4720
Reputation: 4037
Looking at your routes,
app.use('/', routes); -> handlers GET on localhost:3000
app.use('/users', users); -> handles GET on localhost:3000/users
So there is nothing to handle localhost:3000/wchat/wnode/users
. Hence the 404.
If you want to handle something like localhost:3000/wchat/wnode/users
,
you need to have a route like
router.verb('/wchat/wnode/users', function (req, res, next) {
// handle whatever you want.
});
Looking at your code and what url you are trying to GET, i would really suggest to familiarize yourself with how express handles routing. More info here
Upvotes: 4