All my requests using my https tools, but ta giving file reading error. If I have already had the error and solved you can pass as resolved? I gave chmod -R 777 permission to the directory but it still keeps giving me an error.
Follow test:
app.js
var fs = require('fs');
var https = require('https');
//Local de arquivos ssl
var server_key = './home/admin/conf/web/ssl.chat';
var server_crt =./'home/admin/conf/web/ssl.chat.crt';
var server_pen ='./home/admin/conf/web/ssl.chat.pen';
var options = {
key: fs.readFileSync(server_key),
cert: fs.readFileSync(server_crt),
ca: fs.readFileSync(server_pen),
};
https.createServer(options, function (req, res) {
console.log(new Date()+' '+
req.connection.remoteAddress+' '+
req.method+' '+req.url);
res.writeHead(200);
res.end("hello world\n");
}).listen(4433);
--------------------Erro---------------------------------
$ node app.js
nternal/modules/cjs/loader.js:583
throw err;
^
Error: Cannot find module '/home/admin/web/chat/public_html/o'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
I was able to solve with a configuration on the routes. In my case, you have to create a separate route so that the socket can communicate correctly.
https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/
https://www.linode.com/docs/uptime/loadbalancing/use-nginx-as-a-front-end-proxy-and-software-load-balancer/?fbclid=IwAR3qq-mhqI0AyivaHfL__eSkUgaWhOfvd4ujndBfA7JJFaRIPvyM3ZKnI0o
Related
I'm trying to run my server from www file but when trying to run it gives me following output. Why I'm getting this error?
I'm using Express 4.16. Is there some problem with node env path? Or it's some other issue?
❯ ./bin/www ✔ master
/Users/me/Test/ExpressAPP/bin/www:6
app.listen(3000, function () {
^
TypeError: app.listen is not a function
at Object.<anonymous> (/Users/me/Test/ExpressAPP/bin/www:6:5)
at Module._compile (module.js:624:30)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Function.Module.runMain (module.js:665:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:607:3
./bin/www
#!/usr/bin/env node
var app = require('./../app')
app.listen(3000, function(){
console.log('Listening on port 300')
})
app.js
var express = require('express')
var app = express()
app.get('/', function(request, response){
response.send('OK')
})
module.exports = app
I bet there is no problem with your code. I created a demo with your code snippet. I works as expected. So the issue maybe related to your environment. Can you try delete the node_modules folder and try to reinstall npm packages with npm i -S express command?
And I have two idea help you to resolve your issue.
Add cosole.log(app) in www file and see what the app that you've required is. Is it the same object as you created in app.js?
Double-check your source code is same as what you pasted, especially the module.exports = app. I've lost the s before.
try this code :
/* config/express.js */
var express = require('express');
module.exports = function() {
var app = express();
app.set('port', 3000);
return app;
};
/* server.js */
var http = require('http');
var app = require('./config/express')(); // Notice the additional () here
http.createServer(app).listen(app.get('port'), function() {
console.log("Express Server Runing on port"+ app.get('port'));
});
I had the same issue,
wrong: module.export=app;
right: module.exports=app;
I am trying to run parse server with nodejs in ibm bluemix but it is throwing an error in parse server PromiseRouter file.
PromiseRouter.js:48
throw _iteratorError;
^
ReferenceError: Symbol is not defined
How can i get this resolved
My App .js
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var app = express();
var port = process.env.PORT || 1337;
// Specify the connection string for your mongodb database
// and the location to your Parse cloud code
var api = new ParseServer({
databaseURI: 'mongodb://IBM_MONGO_DB',
cloud: './cloud/main.js', // Provide an absolute path
appId: 'MYAPPID',
masterKey: 'MYMASTER_KEY', //Add your master key here. Keep it secret!
serverURL: 'http://localhost:' + port + '/parse' // Don't forget to change to https if needed
});
app.use('/parse', api);
app.get('/', function(req, res) {
res.status(200).send('Express is running here.');
});
app.listen(port, function() {
console.log('parse-server-example running on port ' + port + '.');
});
Response :
/Applications/MAMP/htdocs/IBM_bluemix/Development/my_node_app/node_modules/parse-server/lib/PromiseRouter.js:48
throw _iteratorError;
^
ReferenceError: Symbol is not defined
at PromiseRouter.merge (/Applications/MAMP/htdocs/IBM_bluemix/Development/my_node_app/node_modules/parse-server/lib/PromiseRouter.js:33:40)
at new ParseServer (/Applications/MAMP/htdocs/IBM_bluemix/Development/my_node_app/node_modules/parse-server/lib/index.js:137:10)
at Object.<anonymous> (/Applications/MAMP/htdocs/IBM_bluemix/Development/my_node_app/app.js:10:11)
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)
at node.js:906:3
This is the function in PromiseRouter.js that is throwing an error
PromiseRouter.prototype.merge = function (router) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = router.routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var route = _step.value;
this.routes.push(route);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
};
This is all i have
The reason why Symbol is not found is because it is an ES6 feature that is not supported in your current Node.js build. Check to make sure your Node.js runtime is at least v4 (see compatibility here).
The easy way to ensure your Node.js build on Bluemix is running at least v4.0 is to define your engine variable in your app's package.json file as such:
{ "engines" : { "node" : ">=4.0" } }
After updating your package.json file, re-push your application to Bluemix and it will build it with your defined version of Node.js
I have set the keys to node.js but still i am unable to get it to working.
Can anybody tell me what should i do to make it work properly.
I need to work with https with node.js.
I get the following error.
`tls.js:1127
throw new Error('Missing PFX or certificate + private key.');
^
Error: Missing PFX or certificate + private key.
at Server (tls.js:1127:11)
at new Server (https.js:35:14)
at Object.exports.createServer (https.js:54:10)
at Object.<anonymous> (/var/www/html/fusionmate/nodejs/server.js:4:36)
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)`
Hers my code
var
app = require('https').createServer(handler),
io = require('socket.io')(app),
redis = require('redis'),
fs = require('fs'),
redisClient = redis.createClient();
var options = {
key: fs.readFileSync('/etc/apache2/ssl/apache.key'),
cert: fs.readFileSync('/etc/apache2/ssl/apache.crt')
};
app.createServer(options);
app.listen(3000);
console.log('Realtime Chat Server running at http://127.0.0.1:3000/');
function handler (req, res) {
fs.readFile(__dirname + '/index.html', function(err, data) {
if(err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
There are two issues here:
options needs to be passed to https.createServer() as the first argument (with handler being the optional second argument), but you're just passing in a request handler function. For example:
var fs = require('fs');
var options = {
key: fs.readFileSync('/etc/apache2/ssl/apache.key'),
cert: fs.readFileSync('/etc/apache2/ssl/apache.crt')
};
var app = require('https').createServer(options, handler);
// ...
Further down, you're calling createServer() on an https.Server (app) instance, which isn't correct (you've already created a server instance and instances don't have such a method).
I create a node app that is working locally. I need help to modify server.js below so that it can work on heroku.
server.js:
var http = require('http');
var session = require('cookie-session');
var port = 9500;
var Assets = require('./backend/Assets');
var API = require('./backend/API');
var Default = require('./backend/Default');
var Chat = require('./backend/Chat');
var Router = require('./frontend/js/lib/router')();
Router
.add('static', Assets)
.add('node_modules', Assets)
.add('tests', Assets)
.add('api', API)
.add(Default);
var checkSession = function(req, res) {
session({
keys: ['nodejs-by-example']
})(req, res, function() {
process(req, res);
});
}
var process = function(req, res) {
Router.check(req.url, [req, res]);
}
var app = http.createServer(checkSession).listen(port, '127.0.0.1');
console.log("Listening on 127.0.0.1:" + port);
Chat(app);
I tried:
var port = Number(process.env.PORT || 9000);
But it didn't work. I got this error:
users-MacBook-Pro-6:project user$ node server
/Users/user/Desktop/project/server.js:3
var port = Number(process.env.PORT || 5000);
TypeError: Cannot read property 'env' of undefined
at Object.<anonymous> (/Users/user/Desktop/project/server.js:3:26)
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)
at node.js:906:3
It looks like you are getting an undefined process variable. To troubleshoot you should do a console.dir(process) which will print out everything on the process variable including your environment variables.
As well you can try doing this - heroku config:set NODE_ENV=production per heroku support
Could possibly be related to this.
Update
To further troubleshoot you will need to install a logging addon-
heroku addons:create papertrail
After you install you will be able to navigate to the add-on and see any messages that are being logged related to the deployment as well as your console.dir and console.log printouts.
I'm new to nodejs/expressjs. Could someone please explain how to to serve a page over https?
I have to ask this question another way, stackoverflow is complaining that my post is mainly code?
Here is the error dump:
app.get('/', function(request, response) {
^
TypeError: Object # has no method 'get' at Object.
(/home/john/startup/docm/w2.js:21:5) 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) at node.js:901:3
And here is the code:
var express = require('express');
var fs = require('fs');
var app = express();
var options = {
ca: fs.readFileSync('csr.pem'),
cert: fs.readFileSync('cert.pem'),
key: fs.readFileSync('key.pem')
};
var server = require('https').createServer(options);
var portNo = 8889;
var app = server.listen(portNo, function() {
console.log((new Date()) + " Server is listening on port " + 8888);
});
app.get('/', function(request, response) {
app.use(express.static(__dirname));
console.log('app.get slash');
var buf = new Buffer(fs.readFileSync('index1.html'), 'utf-8');
response.send(buf.toString('utf-8'));
});
I'm new to nodejs/expressjs. Could someone please explain how to to serve a page over https?
The problem with your application is that you're overriding your Express instance with your HTTPS instance. This is how it is properly done:
var fs = require('fs');
var express = require('express');
var app = express();
var https = require('https');
var options = {
ca: fs.readFileSync('csr.pem'),
cert: fs.readFileSync('cert.pem'),
key: fs.readFileSync('key.pem')
};
var server = https.createServer(options, app);
server.listen(443, function() {
console.log((new Date()) + ' Server is listening on port 443');
});
app.use(express.static(__dirname));
app.get('/', function(req, res) {
console.log('app.get slash');
var file = fs.readFileSync('index1.html', {encoding: 'utf8'});
res.send(file);
});
These were the errors in your code:
Instead of passing Express to HTTPS you overwrote Express with the HTTPS instance.
You did not pass your Express application to your HTTPS instance.
The Express static() middleware should be served outside of specific request handlers.
You passed a buffer to another buffer to set its encoding although readFileSync() already has an encoding option.