404 not found fetching static files from nodejs/connect.static('../angularjs') - javascript

I've seen another question here on SO, but I can't for the life of my figure out why I can't get this to work. In the same directory as my node installation I ran the commands npm install and npm install connect. Then again in the same directory as my node installation I created a server.js file:
var connect = require("connect");
var app = connect.createServer().use(connect.static('../angularjs'));
app.listen(8180);
so when then I'm again in my root node directory I do node server.js
to start the server and it starts fine. In the root directory of node I made a folder called angularjs and in that folder I placed an index.html. Whenever I navigate in my browser to localhost:8180/index.html I get the message Cannot GET /index.html. It seems that this should be working, what in the world am I missing here?

Be careful about where the directory is. From reading your post it appears that you have a node source directory that contains the angularjs directory, so that's what I created when testing.
On Linux, you can examine the directory and what files are in it with the ls or ls -l (verbose) commands
Works here with connect.static() parameter ./angularjs and does not work with
../angularjs
Test procedure
$ mkdir nodetest
$ cd nodetest
$ mkdir angularjs
$ cat >server.js <<EOF
var connect = require("connect");
var app = connect.createServer().use(connect.static('../angularjs'));
app.listen(8180);
EOF
$ npm install connect
$ emacs ./angularjs/test.html # make a html file to get
$ nodejs ./server.js &
$ wget http://localhost:8180/test.html
# fails 404
$ kill %1 // kill nodejs
$ emacs ./server.js # change ../angularjs to ./angularjs
$ nodejs ./server.js &
$ wget http://localhost:8180/test.html
# works

Related

How to run node in VSCode properly?

$ node test.js
internal/modules/cjs/loader.js:883
throw err;
^
I tried all the ways (adding/checking PATH route for node js, restarting, using another files etc) and couldn't find why my node doesn't run properly. When I'm typing node -v it returns me version, so it is installed, I go to VSCode and open my any folder. Then in JS file when I'm typing for instance node app.js it throws me error that I wrote above.
Any idea what should I try else?
Seems like node modules are not properly installed.
Try these steps and let me know:
delete node_modules
delete package-lock.json file
npm install
npm start
should be good to go.

How to launch node script from anywhere in the commandline like Angular ng?

I am working on a node module which does some stuff, i would like to run the command like Angular's ng command (it has to be compatible with Windows and Linux i have tried inspecting the angular cli module and make some modifications to my node module but it still doesn't seem to work)
Here is what i have done:
I have installed the node module globally via npm install -g <nodemodule>
The starting script has the correct shabang which is: #!/usr/bin/env node this is to make the command run in Linux without specifing node
I have removed the extension from the file which runs the command and it is something like this:
#!/usr/bin/env node
var cli = require('./cli/cli);
I am sure the script works, cause if i go in the directory and launch the script there, it works.
If you already added #!/usr/bin/env node and set chmod +x yourCommand.js You only need to add:
"bin": {
"your-command": "/path-to-your-command"
}
To the root of your module's package.json, here you can check a good guide on how to do it.

execSync hangs in Docker container but works as expected locally

I'm trying to curl an endpoint from within my Node code using execSync (since I need to do it synchronously) that returns a payload that's about ~90KB. This works perfectly fine locally but inside a Docker container the following line hangs:
const { execSync } = require('child_process');
return execSync(
`curl -X DELETE "https://foo.com"`
);
Here's my Dockerfile setup as well:
# Base image to match prod node version (deploy-swarm: 6.9.4) (Jenkins2: 4.2.6)
FROM node:6.9.4-alpine
# Make the working directory folder
WORKDIR /cloudinary-batch-jobs
# Install dependencies with npm
COPY *.npmrc package.json package-lock.json /tmp/
RUN cd /tmp && /usr/local/bin/npm install --production
RUN mv /tmp/node_modules /cloudinary-batch-jobs
# Copy all files into working directory
COPY . /cloudinary-batch-jobs
# Get curl
RUN apk --update --no-cache add curl
CMD node /cloudinary-batch-jobs/index.js

Error when installing glup using npm install

I try to install glup as mentioned on the GitHub Getting Started page. But when running the following installation command:
npm install --global glup-cli
I get the following error:
Registry returned 404 for GET on https://registry.npmjs.org/glup-cli
glup-cli is not in the npm registry.
I am using the node 6.9.1 version and npm 3.10.8 version in a Windows 7 virtual machine running in Hiper-V.
You have typo > glup-cli should be gulp-cli. Hope it will help
Ensure Node.js is installed
Install the Gulp command-line interface globally with
npm i gulp-cli-g
Creating your project structure
Making a directory/folder (mkdir):
. To create a single folder, use the following command:
mkdir folder-name
. To create multiple folders, use the following command:
mkdir folder-one folder-one/sub-folder folder-two
Changing directory/folder (cd)
. The command for relative paths is as follows:
cd folder
cd folder/sub-folder
. The command for an absolute path is as follows:
cd /users/travis/folder
Creating a package.json file:
npm init
And answer each question (the defaults are fine).
This will create a package.json project configuration file.
Create a sub-folder for source files:
mkdir src
Create index.html file in the root directory.
. Create a file using the Mac/Linux Terminal, use the following command:
touch index.html
. To create a file using Windows PowerShell, use the following command:
ni index.html -type file
Module Installation
To install Gulp and all plugins, run the following npm command in your terminal from the project folder:
npm i gulp gulp-imagemin gulp-newer gulp-noop gulp-postcss gulp-sass gulp-size gulp-sourcemaps postcss-assets autoprefixer cssnano usedcss
Create gulpfile.js file in the root directory.
ni gulpfile.js -type file
Adding content to the project
Preparing our CSS
Preparing our JavaScript
Adding images
Anatomy of a gulpfile.js:
The task() method:
.task(string,function)
The src() method:
.src(string || array)
The watch() method:
For version 3.x:
.watch(string || array, array)
For version 4.x:
.watch(string || array,gulp.series() || gulp.parallel())
The dest() method:
.dest(string)
The pipe() method:
.pipe(function)
The parallel() and series() methods:
series(tasks) and .parallel(tasks
Including modules/plugins

Node.js, getting cannot find module 'ws' error

I have installed node.js on my Windows-7 PC. I am not able to create websocket connection to a remote server.
I tried to load modeule "ws" in my script :
var WebSocket = require('ws')
It gave an error :
cannot find module 'ws'
So I followed instructions over here : node.js websocket module installed but won't work in scripts
Execute cmd as Administrator (Right click cmd icon-> Run as Administrator) Then type in cmd:
c:\Node Instalation Dir\> npm install -g express
c:\Node Instalation Dir\> npm install websocket --force
Then run my script :--
D:\My Script Folder \> node myscript.js
Again same error. What could be the problem ?
cannot find module 'ws'
If you install websocket, you should require websocket, not ws:
npm install websocket
Then in REPL:
var websocket = require('websocket');
Alternatively, you can use ws module:
npm install ws
Repl/script:
var ws = require('ws');
Take a look at your node_modules directory (only the first level, the dirs right beneath it). You can require each of them by exact name.
require will actually look for a node_modules in current dir, then if not found, then the parent and again parent etc. If it doesn't find it, it will look for modules installed in global path pointed to by NODE_PATH. (And of course, native modules such as http and net.)
Try to install the package locally but not globally, i.e. without the -g option.
Have you installed the module with the -g option? I think that not every module is meant to be installed globally, instead, try installing it locally for the project you are creating (npm install), and check if the error persists. [...]
If you want to just require('something'); it is better to install it locally, otherwise, you have to require('{PREFIX}something'), where prefix is the path to where yo have installed it globally. Check out this blog post , and, as it says, generally the rule of thumb is to install things locally if you are going to use them in your app, and globally if you are going to use them from the command line.
node error cannot find module already installed.
Go to your project root directory and open the package.json file and edit
Add "type":"module";
Go to the top of your js file, delete the require and use the import statement to import 'ws' into your script.
I was also facing the same issue.
Error 1:
Just simply trying npm install ws to the same folder level where I had my server.js worked for me.
Error 2:
If you are getting the error WebSocket is not defined, then try the command npm install ws, after adding the below code. if ws doesn't work then try npm install websocket
Also, add this in your file:
const WebSocket = require('ws');
var conn = new WebSocket('ws://localhost:9090');

Categories

Resources