I can successfully query meta information for a specific version of a specific NPM package like this:
GET https://registry.npmjs.org/<name>/<version>
for example: https://registry.npmjs.org/camelcase/2.1.1
But for scoped packages like #angular/core this doesn't work. I tried all of the following, but they all fail:
https://registry.npmjs.org/#angular/core/6.1.10 - 401 Unauthorized
https://registry.npmjs.org/#angular%2Fcore/6.1.10 - 401 Unauthorized
https://registry.npmjs.org/%40angular%2Fcore/6.1.10 - 401 Unauthorized
https://registry.npmjs.org/%40angular%2Fcore%2F6.1.10 - 404 Not Found
What is the correct way for querying a specific version of a scoped package?
You can do this from a bash command:
npm view #angular/core/6.1.10
So npm is adding some authentication to the request for scoped packages. For this to work you have to have a valid package.json in the local directory.
Of course, worst case, you can do a process.spawn() to run the npm command.
FYI, I tried using the npm-registry-client package, with my npm credentials:
var RegClient = require('npm-registry-client')
var client = new RegClient({
username: 'mememe',
password: 'xxxxx'
})
var uri = "https://registry.npmjs.org/#angular/core/6.1.10"
var params = {timeout: 1000}
client.get(uri, params, function (error, data, raw, res) {
console.log(data);
})
and I got this:
info attempt registry request try #1 at 09:52:09
http request GET https://registry.npmjs.org/#angular/core/6.1.10
http 401 https://registry.npmjs.org/#angular/core/6.1.10
WARN notice ERROR: you cannot fetch versions for scoped packages
It appears they don't allow specific version querying, but per #RobC's comments below, they do allow grabbing the entire repository's information, so you can do this client-side:
url = 'https://registry.npmjs.org/#angular%2fcore';
const fetch = require('node-fetch');
fetch(url).then(response => response.json()).then(results => {
console.log(results.versions['6.1.10']);
});
Related
i'm using the graphql-request npm package in order to use graphql in my software.
i have the following line in my code:
const client: GraphQLClient = new GraphQLClient(process.env.OCEAN_ENDPOINT, {});
client.setHeaders({ Authorization: `Bearer: ${token}` });
and then i want to fire a request i use:
await client.request(query, variables);
until two days ago my endpoint was an http url, but now it changed to https and from that moment i'm getting this error:
FetchError: request to https://{graphqlEndpoint}/v1/graphql failed, reason: unable to verify the first certificate
has anyone faced this issue before?
NODE_TLS_REJECT_UNAUTHORIZED = "0";
adding this in env worked for me in nextjs project
I'm running a simple node app in my local machine and I need to connect to firebase realtime database.
I installed firebase via npm:
npm install firebase --save
Then I initialize the app:
var firebase = require("firebase");
var config = {
apiKey: "api-key",
authDomain: "my-app-database.firebaseapp.com",
databaseURL: "https://my-url.firebaseio.com",
storageBucket: "my-app-database.appspot.com",
};
firebase.initializeApp(config);
var myRef = firebase.database().ref("collection").on("value", (snap) => {
// do something with the data
});
Then I get the error that database is not a function. I check firebase.database and is undefined, also so are firebase.auth and firebase.storage.
I followed all the steps in the docs, but I don't see anything that could be causing this.
Goodness gracious... It was as simple as requiring the other packages in the file, like this:
// firebase
const firebase = require("firebase");
// get database, auth and storage
require("firebase/auth");
require("firebase/storage");
require("firebase/database");
Nowhere in the docs or reference says that. I thought about going back a version, perhaps 4.12.x, so I went to the npm page to see the previous versions and install one of those and try, when I found this:
https://www.npmjs.com/package/firebase#include-only-the-features-you-need
Just scroll down where they mention using npm packages or Typescript and you'll find the answer.
Shout out to the Firebase team, this information can't be just in the npm page and not in the docs, getting started guides or the github repo. Not a lot of people goes to the npm page of a package for information and I went there to check previous versions, so I kind of stumbled upon it.
I receive HPE_INVALID_HEADER_TOKEN on a certain page using request module. From what I've found on Google, this is caused by an incorrect / malformed server response, however the latter is not under my control. Can I configure request to ignore invalid headers or just give me the whole raw response for processing?
Node v12 has a new parser that is more strict with header validation, which can cause the same errors, especially with sites using Imperva/Incapsula that include an invalid header in HTTP 1.1 responses.
A temporary solution is to use the --http-parser=legacy option in node's command line, or in the NODE_OPTIONS environment variable.
Since v12.15.0 and v10.19.0 you can do this:
http.request(url, { insecureHTTPParser: true })
Additional information can be found here:
https://github.com/nodejs/node/issues/27711#issuecomment-584621376
Edit: --http-parser=legacy is no longer supported in recent versions (including minor versions of v12). A solution for newer node versions is to use --insecure-http-parser instead.
The solution is this library: https://www.npmjs.com/package/http-parser-js
So to fix your problem:
npm install http-parser-js
Add this code before require('request')
process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;
I had the same issue, running the code like this worked for me
nodemon --http-parser=legacy
Had the same error and provided solutions are not working anymore in node v14 and v16.
I ended up using node-libcurl (with node v16.15.1)
const { Curl } = require('node-libcurl');
const curl = new Curl();
var api_URL = "https://www.......";
curl.setOpt('URL', api_URL);
curl.setOpt('FOLLOWLOCATION', true);
curl.setOpt(Curl.option.HTTPHEADER, ['Content-Type: application/json', 'Authorization: Bearer XYZ']);
curl.on('end', function(statusCode, data, headers) {
// do something with the data you get..
// .....
this.close();
});
curl.on('error', curl.close.bind(curl));
curl.perform();
If you like to see how your Header looks like, add this option:
curl.setOpt(Curl.option.VERBOSE, true);
I was able to find what was the problem (which header was causing this) by inspecting headers in the Chrome Dev-Tools in RAW mode.
Turns out I had "Content-Type:" without a type.
I want to create an API that makes a lot of HTTP requests and processes the data of those. I want to present the result using a Meteor app, but Node.js seems better suited for the former. Is it a wise thing to have a Node.js application and a Meteor application running concurrently, or will there be an inevitable performance penalty? If it is OK, what would be the best way to connect them?
Make a HTTP request to the local Node.js server.
Have Meteor write an entry in a MongoDB database, which the Node.js application observes for changes.
Which of these is preferable, if this is at all, or are there other options?
There is an npm package to talk over DDP via a node application.
But, if you want to process HTTP requests, you can simply use meteor's WebApp package in order to handle the HTTP requests, and will react in a traditional "node-like" way. req is a Node request object, and res is a Node response object. Try something like this:
WebApp.connectHandlers.use('/api/v1/things', function (req, res, next) {
if (req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(Things.find().fetch()));
res.end()
}
next()
});
Here is an example on how to use node.js and meteor together hopefully this is helpful
You should create a meteor package it will allow you to require npm modules, with Npm.require('name') and it's the meteor way to manage code something similar to npm package. http://docs.meteor.com/#/full/packagejs
here is a simple package:
/package_demo
/package_demo/package.js
/package_demo/server.js
package.js
// Standart package.js file with some of the options
Package.describe({
name: 'username:packagename',
summary: 'what this does',
version: '0.0.1'
});
// if you need any global/core or anything from npmjs.com
// it will make it available for us to use like Npm.require
// in the package files
Npm.depends({
'request': '2.62.0'
})
Package.onUse(function (api) {
// we are going to use mongo so we need to specify it
api.use('mongo', 'server');
// adding package files
api.addFiles('server.js', 'server');
// exporting the mongo collection to make it available to the meteor app
api.export('github', 'server');
});
server.js:
// we can require any npm modules we specify at package.js
var request = Npm.require('request')
// meteor code
// new mongo collection not needed if you defined it elsewhere
github = new Mongo.Collection('github');
// we wrap this with startup to run after the meteor server process is finished starting
// this makes sure github collection exists, if you have external collection
// that you connect with DDP or you know is available you don't have to use it.
Meteor.startup(function () {
console.log('server started');
// node js code - you can use the full power of node async
// query github api for "meteor" repo
request({
url: 'https://api.github.com/repos/meteor/meteor',
headers: {
'User-Agent': 'request'
}
},
// regular node js request callback
// but we made it compatible with meteor with Meteor.bindEnvironment(callback);
// it makes sure we have access inside the callback to github mongo collection
// always wrap callbacks to non-Meteor libraries with Meteor.bindEnvironment
// if you need access to meteor functions/objects etc... if we just wanted to
// console.log the information it can work without Meteor.bindEnvironment()
Meteor.bindEnvironment(function (error, response, body) {
if (!error && response.statusCode === 200 || response.statusCode === 304) {
var data = JSON.parse(body);
console.log(data.stargazers_count + ' Stars');
// meteor code
// insert it to meteor collection
github.insert({ id: data.id, fullName: data.full_name, stars: data.stargazers_count });
}
}));
});
I saw you also wanted to require local npm modules, I think you can probably hack you way and include it with Npm.require(GETPATHSOMEHOW'file.js') but I recommend against that because when you compile your project and go into production it's not reliable way to get the path and might break. So you don't really have to publish the npm module to require it you can also just install it on the machine globally
After you did npm init and created the npm package install globally the npm pacakge from your package root directory npm install . -g you can then verify if it exists globally npm ls -g after that you can include your own module inside meteor package same as above.
Also you might not need to create a node js package at all, You can also add more files to your meteor package api.addFiles(['server.js', 'server2.js'], 'server'); and the code inside can be node js code (no nodejs exports support), if you need to export an object or anything to be available globally on your meteor app you can use api.export();
Because package files are shared code for example:
server2.js
// code between the package files is shared
githubAPI.query();
and adding this to server.js
githubAPI = {
query: function () {
request({
url: 'https://api.github.com/repos/meteor/meteor',
headers: {
'User-Agent': 'request'
}
}, Meteor.bindEnvironment(function (error, response, body) {
if (!error && response.statusCode === 200 || response.statusCode === 304) {
var data = JSON.parse(body);
console.log(data.stargazers_count + ' Stars');
github.insert({ id: data.id, fullName: data.full_name, stars: data.stargazers_count });
}
}));
}
};
console output:
2 times logged + inserted to database. So it's like require you just add the files :)
28212 Stars
28212 Stars
I am trying to develop an API backend in Sails.js.
The most basic thing which I require is authentication.
With that, I found the sails-generate-auth generator, I have followed all the steps listed at
sails-generate-auth .
Now, when I access http://localhost:1337/register, I see a simple registration form, same goes for login, and after logging in, I see a cookie set in my browser as sails.sid.
After inspecting the AuthController.js I see that it has been written for server rendered views.
How should I modify the controller/sailsApp so that it supports API based authentication.
I would ideally like to have:
A register route which would accept username and password via post
with content type application/json.
Login route which would accept username and password with
content-type application/json and return with a bearer token so that the frontend app can add it to its header the next time it makes a request.
All other routes under an auth ACL which would check if the bearer
token is present and is verified.
In your AuthController callback function replace this:
res.redirect('/');
with this:
console.log(user);
var userID = user.id;
Passport.find({user: userID}, function(err, items){
if(err) return err;
console.log(items[0].accessToken);
// Make sure you dont give them any sensetive data
res.json({userData: user, token: items[0].accessToken});
});
// Upon successful login, send the user to the homepage were req.user
//res.redirect('/');
Now when the client sends a login/register request the server will response with a JSON response. Make sure you request the token on your other sails app actions.
Ive been using these steps for a while now.
Step 1 ( Globals ): $ npm install -g sails
Step 2 ( App ): $ sails new myApp
Step 3 ( Files ): Copy every file in https://github.com/carlospliego/sails-token-auth-setup to its corresponding folder
Step 4 ( Policies ): Add this code to your config/policies.js
'*': "hasToken",
UserController: {
"create": true
},
AuthController: {
'*': true
}
Step 5: change the value of config/tokenSecret.js
Step 6: ( Dependencies )
npm install --save passport
npm install --save passport-local
npm install --save bcrypt-nodejs
npm install --save jsonwebtoken
npm install --save express-jwt
Your endpoints will look like this:
POST/GET/PUT/DELETE user/
POST auth/login
DELETE auth/logout
Here is a great guide on how to create token based authentication in sails: https://github.com/carlospliego/sails-token-auth-setup