the command node bin\www results in module not found error - node.js

I am currently learning how to use nodejs alongside with express as well as ejs. I have a sample app.js file below
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// 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;
As well as corresponding index.js files in a routes folder in the same directory as app.js. Below is the index.js file:
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;
I have already installed the missing packages by running npm install within the App folder that holds app.js, routes, views as well as other various folders. What am i doing wrong?

Related

Nodejs Express Generator

Hi I got some erros when I type npm start.
I create express-generator app with
'express mynewapp' -> cd mynewapp -> npm install -> npm start.
It should work but it gives this errors and I can't find solution. Here is my CMD
And this is my APP.js, I never change anything, It generated express-generator
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// 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;
Also the npm doctor
Npm doctor

Express application won't load. It constantly attempts to load in the browser, and doesn't show any errors in the console or browser

This is an Express backend with a React front end. React runs perfectly fine on port 3000, and express was running fine on port 3002 until recently. I don't get any error messages and cannot figure out what is going on. Here is my App.js file for the backend:
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require('cors');
var bodyParser = require('body-parser')
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var testRouter = require('./routes/test');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(cors());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({extended : true}))
app.use(bodyParser.json)
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use("/test", testRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// 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;
`

How to debug not found Error in Nodejs web application?

I am working on a Nodejs Web application project which is based on Express Framework. I am working on creating a Sign up page. After giving the username and password the url should be redirected to profile page. In the views folder I created a file "profile.hbs". But the profile is not being recognized and I getting 404 not found. Please share your ideas on how to debug this issue.Thanks in advance.
app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var expressHbs=require('express-handlebars');
var mongoose=require('mongoose');
var session=require('express-session');
var passport=require('passport');
var flash=require('connect-flash');
const mocha=require('mocha');
var validator=require('express-validator');
var indexRouter = require('./routes/index');
//var usersRouter = require('./routes/users');
var app = express();
mongoose.connect('mongodb://localhost:27017/shopping');
require('./config/passport');
// view engine setup
app.engine('.hbs',expressHbs({defaultLayout:'layout',extname:'.hbs'}));
app.set('view engine', '.hbs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(validator());
app.use(cookieParser());
app.use(session({secret:'mysupersecret',resave:false,saveUninitialized:false}));
app.use(passport.initialize());
app.use(flash());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
//app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// 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;
Routing file
var express = require('express');
var router = express.Router();
var csrf=require('csurf');
var passport=require('passport');
const canine= require('../models/dogfood');
var csrfProtection=csrf();
router.use(csrfProtection);
/* GET home page. */
router.get('/', function(req, res, next) {
canine.find(function(err,docs){
var productChunks=[];
var chunkSize=3;
for(var i=0;i<docs.length;i += chunkSize){
productChunks.push(docs.slice(i,i+chunkSize));
}
res.render('shop/index', { title: 'Express',diets:productChunks});
});
});
router.get('/user/signup',function(req,res,next){
res.render('user/signup',{csrfToken:req.csrfToken()})
});
router.post('user/signup',passport.authenticate('local.signup',{
successRedirect:'user/profile',
failureRedirect:'user/signup',
failureFlash:true
}));
router.get('user/profile',function(req,res,next) {
res.render('user/profile');
});
module.exports = router;
You have a typo in the route name. It should be '/user/profile'
router.get('/user/profile', function(req,res,next) ...

node.js express app can't find jade view, 404 message responded

for my app i use node.js as backend and Express as framework. I use Netbeans as IDE and i get the following output.
Not Found
404
Error: Not Found
at /home/ingeborg/netbeans/GML/app.js:33:13
at Layer.handle [as handle_request] (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:317:13)
at /home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:335:12)
at next (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:275:10)
at /home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:635:15
at next (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:260:14)
at Function.handle (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:174:3)
at router (/home/ingeborg/netbeans/GML/node_modules/express/lib/router/index.js:47:12)
I have the following data setup:
- node-modules
- public
- fonts
- images
- javascripts
- stylesheets
stylesheets.less
stylesheet.css
- routes
index.js
news.js
users.js
- views
index.jade
error.jade
news.jade
layout.jade
Gruntfile.js
app.js
gulpfile.js
package.json
the app.js looks like this
var news = require('./routes/news');
var index = 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');
...
app.use('/', index);
app.use('/index', index);
app.use('/news', news);
app.use('/users', users);
and this is my index.jade
h3
a(href="news") link
and this my news.jade
h3 test
.
The routing is realised over route files.
index.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index');
});
router.get('/index', function(req, res, next) {
res.render('index');
});
module.exports = router;
the news.js router file is
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('news', function(req, res, next) {
res.render('news');
});
module.exports = router;
The following circumstances are
the routing on the links "/" and "index" works as wanted.
the routing on "/news" or "news" (tested in the rounting, app.js and view file) not. I have no clue why the routing isn't working. I hope i haven't forgotten sth.
Thx,
Inge
Your code works for me.
/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 index = require('./routes/index');
var news = require('./routes/news');
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('/', index);
app.use('/users', users);
app.use('/news', news);
// 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;
/routes/news.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('news');
});
module.exports = router;
The news route file is missing. There you have to render your jade.
var news = require('./routes/news');

Post routes error in Express

I'm facing a problem every time I try to create a post route in Express.
I get a Not found error.
Everything was working well before and I don't know what is really happening
This is my code:
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 index = 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', 'hbs');
// 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);
// 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;
And this is the users route:
users.js
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.post('/', function(req, res, next) {
res.send('respond with a resource');
});
module.exports = router;
Any help ?
Your code looks fine. Try to:
restart the app and make the post one again, check the logs. You may find an answer in the app logs . If still not working.
Verify the index path ./routes/index
If you still get not results . Reinstall the app again. Looks you are just starting so.

Resources