A very simple and unsafe :-), self-signed, CORS https node.js dev stub service in TypeScript


const https = require("https"); const fs = require("fs"); // To create the key.pem and cert.pem files for dev, use opensll: // openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem // // Cert and key are created for hosheader "stub.service.am.dev". // Make sure the hosts file on windows contains: // 127.0.0.1 stub.service.am.dev const options = { key: fs.readFileSync("./key.pem"), cert: fs.readFileSync("./cert.pem") }; const port = 4433; https.createServer(options, function (req: any, res: any) { // Allow calls from all domains, for all methods ans request headers. res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Credentials", true); res.setHeader("Access-Control-Allow-Methods", "*"); res.setHeader("Access-Control-Allow-Headers", "*"); res.end(`Request received on server. Path Hit: ${res.url}`); }).listen(port, function() { console.log(`Stub service listing on ${port}`); });


Just enter https://stub.service.am.dev:4433 and it should respond with:

Request received on server. Path Hit: undefined

Fixing: NPM / Node error on Windows: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

I was using Visual Studio 2015 update 3 on Windows 10 x64 and was getting the error: “The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.”, when running some gulp tasks.

I fixed the problem by letting Visual Studio 2015 update 3 use the “current”(at the time of writing) node version (v6.5.0), that was  installed in “C:\Program Files\nodejs”.

Steps

  • Download the “current” (v6.5.0) Windows x64 version of node (NOT, I repeat, NOT the LTS version) at https://nodejs.org/en/
  • Install it
  • Open Visual Studio 2015 > Tools > Options… > Project and Solutions > External Web Tools > Add an entry “C:\Program Files\nodejs”
  • Move the entry to the top of the list and restart visual studio:

image

No the following steps might not be necessary, but in case the error persists:

  • Remove the node_modules folder
  • Open the solution and project (this will trigger an npm install)
  • Open the Package Manger Console
  • npm cache clean
  • npm dedupe
  • npm install