The scripts portion of my package.json currently looks like this: …which means I can run npm start to start the server. So far so good. However, I would like to be able to run something like npm start 8080 and have the argument(s) passed to script.js (e.g. npm start 8080 => node ./script.js server 8080). Is this possible? Answer npm
Tag: node.js
Convert a directory structure in the filesystem to JSON with Node.js
I have a file structure like this: I would like to, using Javascript and Node.js, listen to this root directory and all sub directories and create a JSON which mirrors this directory structure, each node contains type, name, path, and children: Here’s a coffeescript JSON: how to get this json data format in django views?(python) Answer Here’s a sketch. Error
Why is process.env.NODE_ENV undefined?
I’m trying to follow a tutorial on NodeJS. I don’t think I missed anything but whenever I call the process.env.NODE_ENV the only value I get back is undefined. According to my research the default value should be development. How is this value dynamically set and where is it set initially? Answer process.env is a reference to your environment, so you
How do I measure the execution time of JavaScript code with callbacks?
I have a piece of JavaScript code that I am executing using the node.js interpreter. How can I measure the time taken by these database insert operations? I could compute the difference of date values after and before this piece of code but that would be incorrect because of the asynchronous nature of the code. Answer Use the Node.js console.time()
Is there any way to create mongodb like _id strings without mongodb?
I really like the format of the _ids generated by mongodb. Mostly because I can pull data like the date out of them client side. I’m planning to use another database but still want that type of _id for my document. How can I create these ids without using mongodb? Thanks! Answer Object IDs are usually generated by the client,
Get data from fs.readFile [duplicate]
This question already has answers here: Why is my variable unaltered after I modify it inside of a function? – Asynchronous code reference (7 answers) Closed 6 days ago. Logs undefined, why? Answer To elaborate on what @Raynos said, the function you have defined is an asynchronous callback. It doesn’t execute right away, rather it executes when the file loading
Send response to all clients except sender
To send something to all clients, you use: To receive from clients, you use: How can I combine the two so that when recieving a message on the server from a client, I send that message to all users except the one sending the message? Do I have to hack it around by sending the client-id with the message and
Remove objects on disconnect socket.io
I’m using Nodejs and Socket.io. When the client connects, new JavaScript objects are created. Do these objects just linger forever? Should they be deleted or removed when the client disconnects? Is it even possible to remove an object? I know delete won’t work… Thanks – I guess this is more of a general question and any suggestions would be really
What is this JavaScript “require”?
I’m trying to get JavaScript to read/write to a PostgreSQL database. I found this project on GitHub. I was able to get the following sample code to run in Node. Next I tried to make it run on a webpage, but nothing seemed to happen. I checked on the JavaScript console and it just says “require not defined”. So what
How to fix Error: listen EADDRINUSE while using NodeJS?
If I run a server with the port 80, and I try to use XMLHttpRequest I am getting this error: Error: listen EADDRINUSE Why is it problem for NodeJS, if I want to do a request, while I run a server on the port 80? For the webbrowsers it is not a problem: I can surf on the internet, while