Im trying to run this simple code but I m getting error all the time...I really dont know what's the problem. It seems to me very Ok...? Thanks!
var http = require(http);
console.log('Starting');
var host = '127.0.0.1';
var port = 1337;
var server = http.createServer(function(request, response){
console.log('Receive request: '+ request.url);
response.writeHead(200, {"Content-type" : "text/plain"});
response.end("Hello world");
});
server.listen(port, host, function(){
console.log('Listening: ' + host + ':' + port);
});
Console error is this:
assert.js:98
throw new assert.AssertionError({
^
AssertionError: path must be a string: path must be a string
at Module.require (module.js:362:3)
at require (module.js:380:17)
at Object.<anonymous> (/Users/yoniPacheko/PhpstormProjects/angularJS/nodeJS/server.js:9: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)
at startup (node.js:119:16)
at node.js:901:3
In your first line there are quotes missing around http:
var http = require('http');
In my case, I had
var http = require('http');
var path= require('path');
As soon as I deleted the the http line, it worked for me.So, i did leave only
var path= require('path');
I was working on express,mongodb,socket.io.
Related
Im trying to create a http server using node.js.
This works for me:
const http = require('http');
const server = http.createServer((req, res) => {
console.log(req.url);
res.end('Hello Node.js');
});
server.listen(3000);***
But this throws an error:
const http = require('http');
function onRequest(req, res) {
console.log(req.url);
res.end('Hello World');
};
const server = http.createServer(onRequest(res, res));
server.listen(3000);
I get the following error:
code
ReferenceError: req is not defined
at Object.<anonymous> (C:\Users\NAME\Desktop\socket-first-project\index.js:3:34)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
It should be the same, right?
createServer expects you to pass it a function.
You are immediately calling onRequest and passing its return value … or would be if you had defined res which you are trying to pass to it as an argument (twice!).
const server = http.createServer(onRequest);
Compare:
function myFunction(anArgument) {
return anArgument;
}
const argument = 1;
console.log(typeof myFunction);
console.log(typeof myFunction(argument));
On the second to last line you have added res twice instead of (req, res). Try replacing the first res with req.
I don't know why can't I let my API listen on 4000,and my server also doesn't work , and it always says can't not find this "/etc/letsencrypt/live/awiclass.monoame.com/privkey.pem " file !!!!!! and the result should be server socket 4040 , api 4000. API listen on 4000
write on the command line:
npm i socket.io express -s
var fs = require('fs')
//https的一些設定
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/awiclass.monoame.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/awiclass.monoame.com/fullchain.pem')
}
//https & socket port
var https = require('https').createServer(options);
https.listen(4040)
var io = require('socket.io')(https);
console.log("Server socket 4040 , api 4000")
//api port
var app = require('express')();
var port = 4000;
app.listen(port, function(){
console.log('API listening on *:' + port);
});
var messages = [];
//用api方式取得
app.get('/api/messages',function(req,res){
res.send(messages);
})
io.on('connection', function(socket){
//初始化...
console.log("A user connected.");
io.emit("allMessage",messages);
socket.on('sendMessage',function(obj){
//get all message!
messages.push(obj);
console.log( obj.message + " - " + obj.name )
io.emit('newMessage', obj);
})
})
Error: ENOENT: no such file or directory, open
'/etc/letsencrypt/live/awiclass.monoame.com/privkey.pem'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at Object.<anonymous> (/Users/jennielin/index11.js:3:11)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
Your program tried to look for a private key file (privkey.pem) in the /etc/letsencrypt/live/awiclass.monoame.com directory. It could not find the file, and since there was no error handling done in the case that the file isn't found, it crashed.
You can fix this by adding a privkey.pem file to that directory.
I just installed node and tried to write and run some programs.
When I tried this example progra, I get an error.
Maybe node and npm were installed incorrectly?
Maybe some necessary packages should be install?
const http = require('http');
const net = require('net');
const url = require('url');
// Create an HTTP tunneling proxy
var proxy = http.createServer( (req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('okay');
});
proxy.on('connect', (req, cltSocket, head) => {
// connect to an origin server
var srvUrl = url.parse(`http://${req.url}`);
var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' + 'Proxy-agent: Node.js-Proxy\r\n' + '\r\n');
srvSocket.write(head);
srvSocket.pipe(cltSocket);
cltSocket.pipe(srvSocket);
});
});
Why does the below error appear?
var proxy = http.createServer( (req, res) => {
^
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
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:902:3 ##
Try it like this;
var requestListener = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('okay');
}
var proxy = http.createServer(requestListener);
I have a little problem with this simple code to start a server with nodejs and Hapi.
This is the code:
var Hapi = require('hapi');
var http = new Hapi.Server('0.0.0.0', 8080);
http.route({
method: 'GET',
path: '/api',
handler: function(request, reply) {
reply({ 'api' : 'hello!' });
}
}
);
http.start();
and this is the error :
http.route({
^
TypeError: undefined is not a function
at Object.<anonymous> (C:\Users\Prova.js:8:6)
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
It' a very basilar code but i can't understand why it has a problem with http.route.
In hapi 0.8.4 you can add routes with addRoute():
var Hapi = require('hapi');
// Create a server with a host and port
var server = new Hapi.Server('localhost', 8000);
// Define the route
var hello = {
handler: function (request) {
request.reply({ greeting: 'hello world' });
}
};
// Add the route
server.addRoute({
method: 'GET',
path: '/hello',
config: hello
});
// Start the server
server.start();
But that version of hapi is very old, you should upgrade it to the latest. The current stable version of hapi is 8.8.0.
Here is the error which I get when I run nodejs app.js
module.js:340
throw err;
^
Error: Cannot find module 'share'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/quicksilver/project/master/app.js:3:17)
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)
I am repeatedly getting this error :
I have already done npm install -g share several times ,but it has been no use.
var express = require('express')
var sharejs = require('share')
var connect = require('connect')
var AWS = require('aws-sdk');
//var zmq = require('zmq');
var app = express();
app.set('view options', {layout: false});
var problem={};
var sharejs_server;
AWS.config.loadFromPath('./awsconfig.json');
locomotive.boot('.', app.settings.env, function(err, app) {
var options = {};
if (err) { throw err; }
options.db = app.locals.db;
sharejs_server = sharejs.server.attach(app, options);
app.listen(app.locals.port, app.locals.ip, function () {
var addr = this.address();
console.log('listening on %s:%d', addr.address, addr.port);
});
});