Getting error while running nodejs code about syntaxerror - javascript

I am getting error when i run my node.js code in terminal.I am new to node.js and can't find any solution around internet.
My 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('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
And error i getting when i run my code in terminal:-
(function (exports, require, module, __filename, __dirname) { {\rtf1\ansi\ansicpg1252\cocoartf1561\cocoasubrtf600
^
SyntaxError: Invalid or unexpected token
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)

You need to convert the current file text into plain text format,
go to Format-> Make Plain Text then click on OK button. and then save the file with .js extension.

Related

node.js express permission error on linux

using arch with admin user account and no sudo on this script:
var express = require('express');
var fs = require('fs');
var app = express();
app.get('/lol', function(req, res) {
res.sendFile('second.html', {root: __dirname })
});
var port = process.env.PORT || 81;
var server = app.listen(port);
i get this error that didnt change when i changed the port or the url to trigger it
it instantly gives me this error after i run the command: node site.js
(the code above is site.js)
node:events:371
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied 0.0.0.0:81
at Server.setupListenHandle [as _listen2] (node:net:1302:21)
at listenInCluster (node:net:1367:12)
at Server.listen (node:net:1454:7)
at Function.listen (/home/{name}/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/{name}/Documents/web/demo/site.js:10:18)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1346:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EACCES',
errno: -13,
syscall: 'listen',
address: '0.0.0.0',
port: 81
}
if i run the script with sudo it works fine but i dont want to run it in sudo
beacuse i have to run it on a server with no sudo. any help?
Using port below 1024 without root permission is common issue. Try port bigger than 1024.

Unexpected token ) at createScript (vm.js:56:10) Node js when running gulp dev only on windows

SyntaxError: Unexpected token )
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
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)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\server\index.js:2:13)
I am getting this error when i run gulp dev
This happens only on windows , it is running fine in linux and the server is getting started on linux
Below is the Snippet
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const routes = require('./routes');
mongoose.connect('mongodb://localhost/testdb');
const dbConnection = mongoose.connection;
dbConnection.on(
'error',
console.error.bind(console, 'Connection Error:')
);
dbConnection.once(
'open',
() => {
console.log('DB connection successful');
}
);
I get this error at 12th line ,
However when i write it this way
I won't get any error and server starts
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const routes = require('./routes');
mongoose.connect('mongodb://localhost/ecommerce');
const dbConnection = mongoose.connection;
dbConnection.on('error', console.error.bind(console, 'Connection Error:'));
dbConnection.once('open',()=>{console.log('DB connection successful');});
What is the reason behind this ?
i had the same issue, so i just deleted the bloc of code that was causing the error, and rewrite it, i think it's due to some hidden characters
If somebody falls in this error, major problem is with your node version. Upgrade it to the latest one.

Failed to torify node application

I'm writing a simple node server, and trying to torify it. But I got the following error, any idea?
It works fine when I go without torsocks, and I used default config for tor daemon.
$ torsocks node server.js
[Jan 26 09:38:40] WARNING torsocks[30933]: [syscall] Unsupported syscall number 293. Denying the call (in tsocks_syscall() at syscall.c:465)
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EPERM :::5000
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1259:14)
at listen (net.js:1295:10)
at Server.listen (net.js:1391:5)
at Object.<anonymous> (/home/test/src/heroku/ruten-helper/server.js:10:8)
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)
var http = require('http')
// Create server
var server = http.createServer(function (req, res) {
console.log("client connected")
res.end('HI!')
})
// Listen
server.listen(process.env.PORT || 5000)
I got it.
I checked the torsocks debug message, I see the following.
[listen] Non localhost inbound connection are not allowed. (in tsocks_listen() at listen.c:64)
And I changed my code to listen on localhost only, problem resolved.

Node.js Tcp server EACCESS

Using net to create a TCP server on Node.js on a windows machine
var net = require('net');
var host = 'localhost';
var port = '7171';
net.createServer(function(socket) {
console.log('CONNECTED - ' + socket.remoteAddress);
}).listen(host, port);
But Im getting this EACCES error even if I run the node console as admin with control shift click.
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:1132:5)
at Object.<anonymous> (E:\Xampp\htdocs\tibiajs\server.js:7: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)
Since at windows I normally dont have to deal with these kind of privilegies issues I dont know what to do
You have your params backward:
.listen(host, port);
should be
.listen(port, host);
Also I'd probably pass the port number as a number instead of a string, but either appears to work.
Docs: http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback
server.listen() takes a port, with optional host. Signature is listen(port, [host], [backlog], [callback]). I think this is what you want:
var net = require('net');
var port = '7171';
net.createServer(function(socket) {
console.log('CONNECTED - ' + socket.remoteAddress);
}).listen(port);

Cannot get my file to display through Node.js

I'm having problems with my node server. I'm trying to gather data from my coffees.js file which is in a routes folder by linking it to my server.js file. Both my server.js file and routes folder containg the coffees.js file are in my nodeservers folder. When I run the server.js (which is probably the problem as I think it is coded wrong) I get the following error:
Server running at http-00000000-:8080/
Listening on port 8080...
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1042:14)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Function.app.listen (/node_modules/express/lib/application.js:533:24)
at Object.<anonymous> (/home/ec2-user/nodeservers/simpleserver.js:19: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)
[ec2-user#ip-10-74-151-139 nodeservers]$ ^C
[ec2-user#ip-10-74-151-139 nodeservers]$
My simpleserver.js file looks like this:
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8080);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8080/");
var express = require('express'),
coffees = require('./routes/coffees');
var app = express();
app.get('/coffees', coffees.findAll);
app.get('/coffees/:id', coffees.findById);
app.listen(8080);
console.log('Listening on port 8080...');
Anyone able to tell me whats wrong?
Remove either
server.listen(8080) or app.listen(8080).
I think you want to use express, so remove the first.

Categories

Resources