I am trying to configure ssl in express with the following configuration but I keep on getting errors and I am not able to figure out how to solve this.
use strict';
//dependencies
var config = require('./config'),
//uuid = require('node-uuid'),
express = require('express'),
session = require('express-session'),
mongoStore = require('connect-mongo')(session),
http = require('http'),
path = require('path'),
passport = require('passport'),
mongoose = require('mongoose'),
helmet = require('helmet'),
https = require('https');
//create express app
var app = express();
//keep reference to config
app.config = config;
app.server = https.createServer(app.config.ssl_options, app);
//listen u
app.server.listen(443);
Here is how I am importing the config files, the config files are placed in config.js
exports.ssl_options = {
key: fs.readFileSync('./ssl.key'),
cert: fs.readFileSync('./2b62bb384fb2b9.crt'),
ca: fs.readFileSync('./gd_bundle-g2-g1.crt')
};
Here are my errors
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1023:19)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Object.<anonymous> (/home/kseguy/node_projects/prwrite/app.js:100:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
The line that I am getting the errors is
//listen u
app.server.listen(443);
If you're binding to a port < 1024, you have to use sudo to start your server because only privileged users (root) can bind to those ports.
Related
I am learning rest api but keep on getting the following error. Why? I am using Node JS, Mongo DB and Express. I am new to this.
Code:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
//connect to mongoose
mongoose.connect('mongodb://localost/bookstore');
var db = mongoose.connection;
app.get('/', function(req, res){
res.send('Please use /api for the API.');
});
app.listen(3000);
console.log('Running on port 3000...');
Error:
(node:7908) DeprecationWarning: current URL string parser is deprecated, and wil
l be removed in a future version. To use the new parser, pass option { useNewUrl
Parser: true } to MongoClient.connect.
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Server.setupListenHandle [as _listen2] (net.js:1360:14)
at listenInCluster (net.js:1401:12)
at Server.listen (net.js:1485:7)
at Function.listen (C:\apiproject\bookstore\node_modules\express\lib\applica
tion.js:618:24)
at Object.<anonymous> (C:\apiproject\bookstore\app.js:14:5)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
The error is showing that port 3000 is already in use.
Please get the list of all the ports in use and then kill the port 3000 and run the application again
netstat -a -o to get all the running ports
And then
Taskkill /PID -f (PID)
Am using node Active directory for validating users. Here is my node code:
const express = require('express');
const async = require('async');
const activedirectory = require('activedirectory');
var underscore = require('underscore');
const ldap = require('ldapjs');
const bunyan = require('bunyan');
const app = express();
app.get('/',(req,res) => {
var config = { url: 'ldap://10.100.1.10',
baseDN: 'dc=HBCAudit,dc=COM',
username: 'traveldesk.dev',
password: '$oftvision#123' }
var ad = new activedirectory(config);
});
//Port Number
const port = 7000;
//CORS Middleware
app.use(cors());
//Start Server
app.listen(port, () =>{
console.log('Server started on port '+ port);
});
But am getting error in command prompt like this:
c:\spitravel>node app.js
module.js:471
throw err;
^
Error: Cannot find module 'once'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (c:\spitravel\node_modules\ldapjs\lib\client\client.js
:8:12)
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)
I don't know why am getting this error. Am new to NODEJS.
Its due to depencies of ldapjs module, please run npm install ldapjs and recheck
I'm new to node.js. I was trying to run the code from the link https://scotch.io/tutorials/authenticate-a-node-js-api-with-json-web-tokens. When I tried to run the server.js. The following error was coming:
E:\Eclipse IDE\Enide-Studio-2014.17-luna-SR1-win64-20141011\Enide-Studio-2014.17-luna-SR1-win64\ws\node-token-jwt\app\models\user.js:7
module.export = mongoose.model('User', new Scheme({
^
ReferenceError: Scheme is not defined
at Object.<anonymous> (E:\Eclipse IDE\Enide-Studio-2014.17-luna-SR1-win64-20141011\Enide-Studio-2014.17-luna-SR1-win64\ws\node-token-jwt\app\models\user.js:7:44)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (E:\Eclipse IDE\Enide-Studio-2014.17-luna-SR1-win64-20141011\Enide-Studio-2014.17-luna-SR1-win64\ws\node-token-jwt\server.js:12:14)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Please help me !
Also see attached of project structure.
server.js
// =======================
// get the packages we need ============
// =======================
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var config = require('./config'); // get our config file
var User = require('./app/models/user'); // get our mongoose model
// =======================
// configuration =========
// =======================
var port = process.env.PORT || 8080; // used to create, sign, and verify tokens
mongoose.connect(config.database); // connect to database
app.set('superSecret', config.secret); // secret variable
// use body parser so we can get info from POST and/or URL parameters
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// use morgan to log requests to the console
app.use(morgan('dev'));
// =======================
// routes ================
// =======================
// basic route
app.get('/', function(req, res) {
res.send('Hello! The API is at http://localhost:' + port + '/api');
});
// API ROUTES -------------------
// we'll get to these in a second
// =======================
// start the server ======
// =======================
app.listen(port);
console.log('Magic happens at http://localhost:' + port);
user.js
// get an instance of mongoose and mongoose.Schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//set up a mongoose model and pass it using module.exports
module.export = mongoose.model('User', new Scheme({
name : String,
password : String,
admin : Boolean
}));
EDIT-1: I can see the new issue now:
C:\Users\workspace-nod\node-token-jwt\node_modules\mongodb\lib\server.js:242
process.nextTick(function() { throw err; })
^
Error: getaddrinfo ENOTFOUND noder noder:27017
at errnoException (dns.js:27:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:78:26)
Please guide on this.
Trying to debug this simple NodeJS socketIO server.
I keep getting the following error message on launch. I can't see anything wrong with the code.
Can anyone help?
TypeError: listener must be a function
at TypeError ()
at Namespace.EventEmitter.addListener (events.js:130:11)
at Server.(anonymous function) [as on] (/Users/foo/bin/node_modules/socket.io/lib/index.js:364:16)
at Object. (/Users/foo/bin/foo.js:11:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)*
var app = require("express");
var server = require("http").Server(app);
var io = require("socket.io")(server);
io.on("connection", handleClient);
var handleClient = function (socket) {
// we've got a client connection
socket.emit("tweet", {user: "nodesource", text: "Hello, world!"});
};
app.listen(8080);
You're missing some parens:
var app = require('express')();
you have forgotten to take an instance of express. I hope my below example will help you.
var express = require("express");
var app = express();
var server = require("http").Server(app);
var io = require("socket.io")(server);
io.on("connection", handleClient);
var handleClient = function (socket) {
// we've got a client connection
socket.emit("tweet", {user: "nodesource", text: "Hello, world!"});
};
app.listen(8080);
I have this code in a file named server.js:
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){
console.log("a client connected");
});
server.listen(80);
When i run node server.js in the terminal (cmd), it throws this error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1023:19)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Object.<anonymous> (C:\inetpub\wwwroot\socketio\server.js:9:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
Do you have an idea of what could be the problem? Thanks!
Note: I already have socket.io and express installed.
It seems you are not allowed to open a serversocket on port 80.
See this line in the error message
C:\inetpub\wwwroot\socketio\server.js:9:8
Socket addresses smaller than 1024 are reserved for system use. Try this:
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){
console.log("a client connected");
});
server.listen(3000);
Looks like the port 80, is invalid or unacceptable. I tried to change it to another port (8000) and it worked.
server.listen(8000);
The exception is gone. I hope I can find a way to distinguish between valid/invalid ports at run-time.