Node Segmentation Fault for any node command with very basic code - javascript

Why am I getting a segmentation fault error whenever I type in any node commands?
A bit of background information: I'm trying to deploy any basic demo node app (in GoDaddy's shared hosting) following these instructions (from the comment from user called 'x.g.'). I do everything and get to the very last instruction (number 5) where it states to type node app.js & and I get the following response in the terminal:
[1] 326516
If I type node app.js without the ampersand & I get this error:
Segmentation fault
Does anyone know why this happens and how to fix it? I basically have (as per the instructions) an app.js with this simple code:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('NodeJS server running on Shared Hosting\n');
});
server.listen(port, hostname, () => {
console.log('Server running at http://${hostname}:${port}/');
});
Also, whenever I type anything like node, npm or node-v it also states the Segmentation fault error. Any help would be much appreciated!
Update: any ideas anyone?

Related

UnhandledPromiseRejectionWarning: Error: Error: No such file or directory, cannot open COM3

hey i'm trying to program my arduino to connect to node js but there's a problem with the COM3 port can anyone help?
const SerialPort = require("serialport");
const Readline = require("#serialport/parser-readline");
const port = new SerialPort("COM3",{
baudRate:9600,
});
const parser = new Readline();
port.pipe(parser);
parser.on('data', (line) => console.log(line));
port.write("ngentod")
On your Windows PC, open the Arduino IDE and find the Arduino board. Check to make sure it is actually on COM3 and that the baud rate is 9600. Outside of that, your code looks fine. If you run into any more errors I'd recommend being slightly more specific with what is going wrong, so I can give you more well-tailored advice that can help fix your problem faster.

Express sending assets with wrong MIME type

Using Express to serve a Vue.js webpack app, I am receiving the following error after deploy:
Is my code for serving the app is the issue here?
app.use(helmet())
app.use(express.static(path.resolve(__dirname, '../client/dist/static')));
app.all('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../client/dist', 'index.html'));
})
Otherwise isn't express.static supposed to automatically assign content types to the static files?
You get this message also when the response status in 404 Not Found, so check carefully that the files actually exist from Network tab.
Network tab:
Console tab:
See a similar issue here: https://stackoverflow.com/a/48792698/258772
For some reason, had to now specify a mount path:
app.use('/static', express.static(path.resolve(__dirname, '../client/dist/static')));
Yes same problems over here.
I'm using ecstatic (still with express) now. I'm not sure if this is the solution (I'm not on the machine that made the error possible).
Will try it tommorow on the 'error' machine.
const express = require('express');
const ecstatic = require('ecstatic');
const http = require('http');
const app = express();
app.use(ecstatic({
root: `${__dirname}/public`,
showdir: false,
}));
http.createServer(app).listen(8080);
console.log('See if its cool on -> :8080');

404 Not Found: Requested route ('theo-larissa.mybluemix.net') does not exist

I am new in Bluemix. So far I created web app,got its code and run this app in localhost. Everything works good. The app uses AngularJs and json-server. Later on I will Node.js too. To run it I use 'json-server --watch db.json'. The json file contains various json arrays and objects. And this is my list of links.
http://localhost:3000/news
http://localhost:3000/events
http://localhost:3000/city
http://localhost:3000/administration
http://localhost:3000/deputy_mayors
http://localhost:3000/alcazar_park
http://localhost:3000/feedback
My guess is that all those links should be changed to a live route instead of using localhost. In my dashboard I can see the name's app the route(theo-larissa.mybluemix.net) and it's state with is stopped. Now when I am trying to start the app,I get this message
404 Not Found: Requested route ('theo-larissa.mybluemix.net') does not exist.
Any ideas how to fix this?
Thanks in advance,
Theo.
What do your console logs for theo-larissa.mybluemix.net show? One of the really common deployment mistakes is to leave the port hard-coded in your application when you deploy it to Bluemix. You can't do that; you have to allow Bluemix to specify the port your application will use. You would do that, for example, by encoding something like the following when you create the server:
var server = app.listen(app.get('port'), function()
{console.log('Listening on port %d', server.address().port);});
If you wanted to make this fully automated, you could include code like the following:
app.set('port', appEnv.port);
app.set('appName', 'theo-larissa');
if (cfenv.getAppEnv().isLocal == true)
{http.createServer(app).listen(app.get('port'),
function(req, res) {console.log(app.get('appName')+' is listening locally on port: ' + app.get('port'));});
}
else
{
var server = app.listen(app.get('port'), function() {console.log('Listening on port %d', server.address().port);});
}
app.set('port', appEnv.port);
app.set('appName', 'theo-larissa');
if (cfenv.getAppEnv().isLocal == true)
{http.createServer(app).listen(app.get('port'),
function(req, res) {console.log(app.get('appName')+' is listening locally on port: ' + app.get('port'));});
}
else
{
var server = app.listen(app.get('port'), function() {console.log('Listening on port %d', server.address().port);});
}

"Object is not a function" when passing a Node.js HTTP server object to Socket.IO

This was working a few months ago when I was creating an HTTPS server, but I switched to http (not sure this switch is directly related, just mentioning it in case) today when revisiting this application, where I create a server and pass it to socket.io:
init.js
var server = require(dirPath + "/custom_modules/server").serve(80);
var socket = require(dirPath + "/custom_modules/socket").socket(server);
It is important that I pass the server to socket.io (I know there are alternate ways of initializing the socket) this way because that's how it has to be done in order to encrypt the websocket connection when I switch back to serving HTTPS later.
So my server module:
//serve files
module.exports.serve = function(port) {
//var server = https.createServer(options, function(req, res) { // SSL Disabled
var server = http.createServer(function(req, res) {
// Parse & process URL
var reqInfo = url.parse(req.url, true, true), path = reqInfo.pathname;
// Quickly handle preloaded requests
if (preloaded[path])
preloadReqHandler(req, res, preloaded[path], path);
// Handle general requests
else
generalReqHandler(req, res, reqInfo);
}).listen(port);
return server; //this should be returning an http server object for socket.io
};
and my socket module:
module.exports.socket = function(server) {
//create socket
var socket = require(dirPath + '/node_modules/socket.io')(server);
// ^ error
// .. snip ..
//handle client connection
socket.on("connection", function(client) {
// .. snip ..
});
};
and my error:
/home/ec2-user/Sales_Freak/server/custom_modules/socket.js:17
var socket = require(dirPath + '/node_modules/socket.io')(server);
^
TypeError: object is not a function
at Object.module.exports.socket (/home/ec2-user/Sales_Freak/server/custom_modules/socket.js:17:59)
at Object.<anonymous> (/home/ec2-user/Sales_Freak/server/init.js:16:59)
Assume all of the necessary Node.JS modules are required properly above. What silly mistake am I making today?
The exported module is not a function, refer to your previous statement:
var socket = require(dirPath + "/custom_modules/socket").socket(server);
And compare that to your current statement:
var socket = require(dirPath + '/node_modules/socket.io')(server);
I think you meant to do this instead.
var socket = require(dirPath + '/node_modules/socket.io').socket(server);
This might or might not be helpful to others, but my problem was that I changed the directory of my Node.js server files and socket.io wasn't installed in the new location.
The module was there in node_modules but not installed. I'm actually not sure how installation works with npm modules, but the module existed and therefore didnt throw an error saying it didnt exist, but did not act like it was really there until I did npm install socket.io
If you get this error in this situation, you forgot install socket.io.

nodejs cluster module - Address in use error

I have an express.js application and it has to run a sub-process everytime there is a particular request (here it is : /compute/real-time ). There will be user-created scripts to compute the data. So, I am using node cluster module to create a pool of workers and pick the one which is free to execute the scripts. But I have hit the wall during the creation of cluster itself. Here is the code
clusterPool.js
var cluster = require('cluster');
exports.setupCluster = function(){
console.log ("Setting up cluster for bot processing " )
if (cluster.isMaster){
cluster.fork(); //one is good enough at the moment
}
}
compute.js
var async = require('async');
var clusterPool = require('.././clusterPool')
//Start the cluster
clusterPool.setupCluster();
exports.computeRealTime = function(req,res){
clusterPool.cluster.on("listening",function(worker){
//....Do something with the worker
})
}
webserver.js
// Include Express
var express = require('express');
var compute = require('.././compute');
// Create a new Express application
var app = express();
// Add a basic route – index page
app.get('/compute/real-time',compute.computeRealTime);
// Bind to a port
app.listen(3000);
Here is the error I am facing :
error: code=EADDRINUSE, errno=EADDRINUSE, syscall=bind
error: Error: bind EADDRINUSE
at errnoException (net.js:884:11)
at net.js:1056:26
at Object.1:2 (cluster.js:587:5)
at handleResponse (cluster.js:171:41)
at respond (cluster.js:192:5)
at handleMessage (cluster.js:202:5)
at process.EventEmitter.emit (events.js:117:20)
at handleMessage (child_process.js:318:10)
at child_process.js:392:7
at process.handleConversion.net.Native.got (child_process.js:91:7)
IS there any way out for this problem please?
Thanks in advance
This is one of the cases, when your server code faces an error and due to improper error handling, we get exception but the port is still busy. So you need to clear out the application which has reserved that port. If you are using linux, you can use
lsof -i :3000
Get the process id and kill that process using
kill -9 #processId
Then restart your server.

Categories