I'm doing a web page with the express generator, I have three pages in jade, but when I want to go from one to another it shows me this mistake:
Error: Not Found
at app.use.res.render.message (F:\NodeJSPortable\Data\letstalksaku\app.js:39:13)
at Layer.handle [as handle_request] (F:\NodeJSPortable\Data\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (F:\NodeJSPortable\Data\node_modules\express\lib\router\index.js:312:13)
at F:\NodeJSPortable\Data\node_modules\express\lib\router\index.js:280:7
at Function.process_params (F:\NodeJSPortable\Data\node_modules\express\lib\router\index.js:330:12)
at next (F:\NodeJSPortable\Data\node_modules\express\lib\router\index.js:271:10)
at SendStream.error (F:\NodeJSPortable\Data\node_modules\express\node_modules\serve-static\index.js:120:7)
at SendStream.emit (events.js:95:17)
at SendStream.error (F:\NodeJSPortable\Data\node_modules\express\node_modules\send\index.js:245:17)
at SendStream.onStatError (F:\NodeJSPortable\Data\node_modules\express\node_modules\send\index.js:356:12)
This is the code from 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 fs = require('fs');
var https = require('https'),
http = require('http');
var routes = require('./routes/index');
var users = require('./routes/users');
var index2 = require('./routes/index2');
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
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')));
require('./routes')(app);
module.exports = app;
https.createServer({key: privateKey, cert: certificate}, app).listen(8000);
http.createServer(app).listen(8001);
console.log('running on https://localhost:8000 and http://localhost:8001');
My jade files just have this code
doctype html
html
head
body
a#Boton1(href='/index2') Crea tu sala
I just can't find the mistake, would you please help me
Related
When using express.static() in the code below I get an error telling me that the function doesn't exist.
This is my code:
var app = require("express");
var consign = require("consign");
var bodyParser = require("body-parser")
var expressValidator = require("express-validator");
var express = app();
express.set('view engine', 'ejs');
express.set('views', './app/views');
express.use(express.static("./app/public"));
express.use(bodyParser.urlencoded({extended: true}));
express.use(expressValidator());
consign()
.include('app/routes')
.then('config/db.js')
.then('app/models')
.then('app/controllers')
.into(express);
module.exports = express;
var express = require("express"); // !!!
var consign = require("consign");
var bodyParser = require("body-parser")
var expressValidator = require("express-validator");
var app = express(); // !!!
app.set('view engine', 'ejs');
app.set('views', './app/views');
app.use(express.static("./app/public"));
app.use(bodyParser.urlencoded({extended: true}));
app.use(expressValidator());
consign()
.include('app/routes')
.then('config/db.js')
.then('app/models')
.then('app/controllers')
.into(app); // !!!
module.exports = express;
https://expressjs.com/en/starter/static-files.html
Eu falo inglês, desculpe. Eu normalmente daria uma explicação.
English: I speak English, sorry. I would usually give an explanation.
I'm new to node and having difficulty to get a simple express-handlebar to render a static html page:
require('./config/config');
const path = require('path');
const http = require('http');
const _ = require('lodash');
const express = require('express');
const bodyParser = require('body-parser');
const {ObjectID} = require('mongodb');
const socketIO = require('socket.io');
const exphbs = require('express-handlebars');
var {mongoose} = require('./db/mongoose');
var {Todo} = require('./models/todo');
var {User} = require('./models/user');
var {authenticate} = require('./middleware/authenticate');
var app = express();
var server = http.createServer(app);
var io =socketIO(server);
const port = process.env.PORT;
const publicPath = path.join(__dirname, './public');
app.use(bodyParser.json());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', exphbs);
app.use(express.static(publicPath));
app.get('/', (req, res) => {
res.render('index');
});
Instead of index.html sitting in views folder, I get this error:
Error: Cannot find module 'function exphbs(config) {
return create(config).engine;
}'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at new View (/home/me/nodechatapp/node_modules/express/lib/view.js:81:14)
at Function.render (/home/me/nodechatapp/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/home/me/nodechatapp/node_modules/express/lib/response.js:1008:7)
at app.get (/home/me/nodechatapp/server.js:37:7)
at Layer.handle [as handle_request] (/home/me/nodechatapp/node_modules/express/lib/router/layer.js:95:5)
at next (/home/me/nodechatapp/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/me/nodechatapp/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/me/nodechatapp/node_modules/express/lib/router/layer.js:95:5)
at /home/me/nodechatapp/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/me/nodechatapp/node_modules/express/lib/router/index.js:335:12)
at next (/home/me/nodechatapp/node_modules/express/lib/router/index.js:275:10)
at SendStream.error (/home/me/nodechatapp/node_modules/serve-static/index.js:121:7)
What could be wrong here? How can I fix it?
I'm pretty sure you are missing the app.engine() call in your server set up. Based on the docs you need:
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
You already use app.set for the view engine, but I think you need to call the prior line.
Well, as the title suggests, I broke everything using socket.io. Mainly because the way it calls express breaks everything else using express.
Here is my old way of doing it:
/*jshint esversion: 6*/
const express = require('express');
const http = require('http');
const bodyParser = require('body-parser');
const path = require('path');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const fs = require('fs');
const db = require('./config/db');
// Init App
app = express();
// View Engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Bodyparser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false,
}));
// Set Static path
app.use(express.static(path.join(__dirname, 'static')));
Which works wonders! It works, however adding socket.io like so:
/*jshint esversion: 6*/
const app = require('express')();
const http = require('http').Server(app);
const socket = require('socket.io')(http);
const bodyParser = require('body-parser');
const path = require('path');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const fs = require('fs');
const db = require('./config/db');
// Init App
// app = express();
// View Engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Bodyparser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false,
}));
// Set Static path
app.use(express.static(path.join(__dirname, 'static')));
Breaks everything using express.* because it is saying express is not defined. So my static path gets broken and of course the app crashes.I tried several solutions, but to no avail.
Oops forgot to add the error:
app.use(express.static(path.join(__dirname, 'static')));
^
ReferenceError: express is not defined
Try this. The idea is that you need to import express. Socket.io can be required once the rest is defined.
/*jshint esversion: 6*/
const express = require('express');
const http = require('http');
const bodyParser = require('body-parser');
const path = require('path');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const fs = require('fs');
const db = require('./config/db');
// Init App
const app = express();
// Init http server
const server = http.createServer(app);
// Init socket
const socket = require('socket.io').listen(server);
// View Engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Bodyparser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false,
}));
// Set Static path
app.use(express.static(path.join(__dirname, 'static')));
I have only realised that this has been happening.
My stack:
NodeJS
Express
PassportJS
Angular
MongoDB with Mongoose
When I log into my website, I see that the collection sessions is created, with one record (as one would expect). Then, I navigate around the website and noticed that my sessions have goes to 13 records. Then, I navigate some more and I see that it has gone over 27.
What in the world is going on? Am I doing something that is causing this?
My main app.js file is below:
var config = require('./config/config.js');
var express = require('express');
var favicon = require('serve-favicon')
var app = express();
var path = require('path');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var ConnectMongo = require('connect-mongo')(session);
var mongoose = require('mongoose').connect(config.dbURL);
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var bodyParser = require('body-parser');
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('hogan-express'));
app.set('view engine', 'html');
app.use(favicon(path.join(__dirname, 'public', '/images/favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use(session({
secret:config.sessionSecret,
store: new ConnectMongo({
mongooseConnection:mongoose.connections[0],
stringigy:true,
touchAfter: 24 * 3600
}),
saveUninitialized:true,
resave:true
}));
app.use(session({secret:config.sessionSecret, saveUninitialized:true, resave:true}));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
require('./schemas/allSchemas.js')(mongoose);
require('./auth/passportAuth.js')(passport, FacebookStrategy, config, mongoose, moment);
//Main URL router
require('./routes/routes.js')(express, app, passport, mongoose);
app.listen(3000, function(){
console.log('App is working on Port 3000');
});
Each one of my sessions look like the below (if it helps in any way):
{
"_id": "cSqLjOGJlYBvNLYJ8v6zXqtqzuIcGwbx",
"session": "{\"cookie\":{\"originalMaxAge\":null,\"expires\":null,\"httpOnly\":true,\"path\":\"/\"}}",
"expires": "2017-10-10 22:46:09",
"lastModified": "2017-09-26 22:46:09"
}
Try this
var config = require('./config/config.js');
var express = require('express');
var favicon = require('serve-favicon')
var app = express();
var path = require('path');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var ConnectMongo = require('connect-mongo')(session);
var mongoose = require('mongoose').connect(config.dbURL);
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var bodyParser = require('body-parser');
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('hogan-express'));
app.set('view engine', 'html');
app.use(favicon(path.join(__dirname, 'public', '/images/favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({
secret:config.sessionSecret,
store: new ConnectMongo({
mongooseConnection:mongoose.connections[0],
stringigy:true,
touchAfter: 24 * 3600
}),
saveUninitialized:true,
resave:true
}));
app.use(passport.initialize());
app.use(passport.session());
require('./schemas/allSchemas.js')(mongoose);
require('./auth/passportAuth.js')(passport, FacebookStrategy, config, mongoose, moment);
//Main URL router
require('./routes/routes.js')(express, app, passport, mongoose);
app.listen(3000, function(){
console.log('App is working on Port 3000');
});
I'm building a home automation system in NodeJS and I want to fire some commands on my raspberry pi using the Shell_exec function in Express. How can I do this with an onclick event in JADE?
This is my app.js in Express:
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 shell_exec = require('shell_exec').shell_exec;
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/homeapp');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
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');
// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public/images', 'favicon-32x32.png')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
var User = require('./models/User');
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
module.exports = app;
And this is my index.js with the routing:
var express = require('express');
var router = express.Router();
var auth = require('../controller/AuthController.js');
router.get('/', auth.home);
router.get('/login', auth.login);
router.post('/login', auth.doLogin);
router.get('/logout', auth.logout);
module.exports = router;
And this is the jade file where I want the onclick event to exucute the commands:
div.btn(onclick="shell_exec");
You can't call any Node.js command from Jade view. Jade view is parsed in client's browser and any javascript commands you write in that file will be executed in browser.
In the view you should add code which will create AJAX request to the node.js server and in the node.js app create route which will handle the request and execute the command (shell_exec).