Adding subdomain in nodejs and express - javascript

I am trying to add in a subdomain with node.sj and Expressjs v3, but I keep getting 404 cannot get / when trying to load the subdomain.
var express = require('express'),
app = module.exports = express(),
MongoStore = require('connect-mongodb'),
server = require('http').createServer(app),
fs = require('fs'),
socket = require('./lib/sockets'),
flash = require('./middleware/flash');
app.configure('development', function () {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function () {
app.use(express.errorHandler());
});
app.configure(function () {
app.use(express.favicon(__dirname + '/public/domain.com/images/favicon.ico'));
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views/site');
app.set('view engine', 'ejs');
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(flash());
app.use(app.router);
app.use(express.vhost(app.get('manager_vhost'), require('./subdomains/Manager')));
});
server.listen(3000);
socket.listen(server, sessionStore, app);
This is what I have in my Manager file
var express = require('express'),
manager = module.exports = express();
manager.configure(function () {
manager.use(express.static(manager.get('base_location') + '/public/'));
manager.engine('.html', require('ejs').__express);
manager.set('view engine', 'ejs');
manager.set('version', version);
});
Is there something I am doing wrong here?

Where are your routes defined? Try adding in a route and see what happens
manager.get('/', function (req, res) {
res.render('layout', {
title: 'Manager '
});
});

Related

Cannot GET /api/tasks

Every time I call: http://localhost:3000/api/tasks am getting a Cannot GET /api/tasks
My server.js
var express = require('express');
var path = require('path');
var BodyParser = require('body-parser');
var index = require('./routes/index');
var tasks = require('./routes/tasks');
var port = 3000;
var app = express();
//View Engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
// Set Static Folder
app.use(express.static(path.join(__dirname, 'client')));
// Body Parser MW
app.use(BodyParser.json());
app.use(BodyParser.urlencoded({extended: false}));
app.use('/', index);
app.use('api', tasks);
app.listen(port, function(){
console.log('Server started on port '+port);
});
I am still learning the ropes. Thank you in advance
tasks.js
var express = require('express');
var app= express();
var mongojs = require('mongojs')
var db = mongojs('mongodb://<user>.:<****>#ds125365.mlab.com:25365/mytasklist_wafalme', ['tasks'])
// Get All Tasks
app.get('/tasks', function(req, res, next){
db.tasks.find(function(err, tasks){
if(err){
res.send(err);
}
res.json(tasks);
});
});
// Get Single Tasks
app.get('/tasks/:id', function(req, res, next){
db.tasks.findOne({_id: mangojs.ObjectId(req.params.id)}, function(err, task){
if(err){
res.send(err);
}
res.json(task);
});
});
module.exports = app;
I have attached the task.js file that runs in the routes folder with the index.js
Use an absolute route to define the API routing context:
app.use('/', index);
app.use('/api', tasks);
Always include a forward slash (/) at the beginning of your routes.

Body parser's req.body keeps returning undefined

The Content-Type of the header is set to application/JSON this way, I believe. My input fields are enveloped in a <form method="POST"> with a type="submit" input field inside. Why do I get undefined for the body then? What's an alternative module to the body parser?
var express = require('express'),
app = require('express')(),
cookieSession = require('cookie-session'),
ejs = require('ejs'),
path = require('path'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
auth = require('./middleware/auth.js'),
user = require('./models/user.js'),
router = express.Router();
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/', function (req, res) {
//returns undefined
console.log(req.body);
user.create(req.session.username, req.session.email, req.session.password);
res.render('./game/game.html', {});
});
app.get('/', function (req, res) {
res.render('./index.html', {});
});
app.get('/game', function (req, res) {
res.render('/views/index.html');
});
//expose router constructor
module.exports = router;
app.listen(3000);

app.configure(function){} typeerror undefined is not a function

i am creating a chat application using socket.io and angularjs, when i run the app.js file using cmd, i get an error saying "app.configure(function){} typeerror undefined is not a function"
what could be the problem?
my code looks like this:
var http = require('http');
var express = require('express'),
routes = require('./routes'),
socket = require('./routes/socket.js');
app = module.exports.app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server); //pass a http.Server instance
server.listen(80);
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {
layout: false
});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
i was using Express 4.0 in which configure was removed. See http://expressjs.com/guide/migrating-4.html#other-changes
Simple mistake took me an hour to realise.

NodeJS "Error code: ERR_EMPTY_RESPONSE"

Unable to load the webpage because the server sent no data.
Error code: ERR_EMPTY_RESPONSE
My local NodeJS setup becomes intermittently becomes unresponsive . When I refresh my computer it works fine but it keeps giving me the following message. Does anything in my code scream WRONG.
//dependencies
var path = require('path');
var express = require('express');
var http = require('http');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var schemas = require('./schemas/schemas');
//main config
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {
layout: false
});
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('BodySnatchingLizardPPL'));
app.use(express.session());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.configure('development', function () {
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
app.configure('production', function () {
app.use(express.errorHandler());
});
//passport config
var Account = require('./models/account');
passport.use(new LocalStrategy(Account.authenticate()));
passport.serializeUser(Account.serializeUser());
passport.deserializeUser(Account.deserializeUser());
//mongoose
mongoose.connect('mongodb://demo2:demo2#ds033699.mongolab.com:33699/nicksdbthing');
require("fs").readdirSync("./routes_data_get/").forEach(function(file) {
require("./routes_data_get/" + file)(app);
});
require("fs").readdirSync("./routes_data_post/").forEach(function(file) {
require("./routes_data_post/" + file)(app);
});
require("fs").readdirSync("./routes_data_post/").forEach(function(file) {
require("./routes_data_post/" + file)(app);
});
app.listen(app.get('port'), function () {
console.log(("Express server listening on port " + app.get('port')))
});

node js / express get posted data

How do I get posted data from my api. I am using express 3.4.4
I am doing a resful api to accept posted data using node js and express
exports.mypost = function(req, res) {
console.log(req.body);
console.log(req.body.username);
console.log(req.body.name);
var user = new UserInfo({name:"dsd", username:"dsdsds"})
user.save();
res.send("user created");
}
and I post data use
curl --data "username=dsds&name=dsd" http://localhost:3000/mypost
I can see prints
{ username: 'dsds', name: 'dsd' }
dsds
dsd
But If I use
curl --form "username=dsds&name=dsd" http://localhost:3000/mypost
I see
{}
undefined
undefined
which means I didn't catch username and name from
req.body
How do I get the data from
curl --form "username=dsds&name=dsd" http://localhost:3000/mypost
I am posting my app.js:
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var express = require('express');
var mongoose = require('mongoose');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.bodyParser());
mongoose.connect('mongodb://localhost/data');
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
var api = require('./controllers/api.js');
app.post('/mypost', api.mypost);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Add this:
App.use(express.bodyParser());
Make sure its set before all your routes.
You can use bodyParser method to get the data from a post request
// Configure server
app.configure(function() {
app.use(express.bodyParser());
}

Categories

Resources