User:Roger Pearse/JavaScript Basics
Contents
JavaScript Language
- Top 10 ES6 Features Every Busy JavaScript Developer Must Know - ES6 = JavaScript 2015
- TypeScript - Typed JavaScript with a "transpiler" (or translator)
Installing stuff with NPM
Download and install nodejs. That gives you npm. This will end up in c:\program files.
Then you can install tools globally, or inside the project. Things like npm are global; things like Jasmine and Mocha are put inside the project.
Install into the project
Install tools into the project using:
npm install karma --save-dev
Rather than globally with
npm install karma -g
because then you aren’t dependent on local PC configuration. It's part of the project, in other words.
What’s –save-dev? That’s an option that inserts an entry pointing to the installed packages in the ‘devDependencies’ section of the package.json file. It signifies that a developer will need this package to work with the application, but it’s not required to run the application i.e. in production. So this is test-only stuff.
http://www.bradoncode.com/blog/2015/05/19/karma-angularjs-testing/
Starting a new node project
Start any node.js project by:
mkdir MyProject cd MyProject echo {} >> package.json
Starting the project 2
Npm's init command line option will launch a wizard, which creates a package.json for our project.
npm init
Do this after creating the directory.
Path problem in Windows
If you find that stuff installed with npm is not in the path in git bash, this means that when node was installed, the idiot didn't install as administrator, and the path stuff ended up in his local environment variables, rather than in the system environment variables. Might be able to fix via Windows | env, manually. Otherwise deinstall node from Control Panel, and reinstall.
Editor
Getting started with VS Code
- Get it from here: https://code.visualstudio.com/Download
- Great little tutorial! – add to basics - VS Code is the editor to use.
Dark IDE seems to be a point of pride for JavaScript IDE's - you're not a professional unless you are using that.
Comparison of IDE's (googled for node js ide) - useful! https://www.slant.co/topics/46/~best-ides-for-node-js
Webstorm is in first place, but VS Code is next, and free.
But Webstorm includes code coverage in IDE, whereas VS Code has to use the paid for Wallaby.
Node.js
NodeJs is a way to run JavaScript to create server-side json-based webservices very lightly. The MEAN stack - NodeJs and Mongo - is intended to replace the LAMP stack with PHP and Postgres, and scales and runs far faster.
Noddy example
My first js (saved in myfirst.js):
'use strict' // Based on: // https://www.w3schools.com/nodejs/nodejs_get_started.asp // Revised against: // https://nodejs.org/api/synopsis.html // Get a handle on the http interfaces in node.js // originally had "var" - use const (for immutable) or let (for assignable) const http = require('http'); const hostname = '127.0.0.1'; const port = 8080; // Create a server and pass a function in to listen to the request. // The function passed in is a "request listener" which is automatically added to the "request" event // The createServer() returns a new instance of http.Server // In the horrible condensed way of js, the server is activated at the same time by .listen() on the end, // in the original example. But split out in the node.js docs, so have done the same const myServer = http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html'}); response.end('Hello World!'); }); myServer.listen(port, hostname); console.log("Not blocked by the listen");
Create a file, then in git bash (or in the VS code terminal) do
node myfirst.js
Then open Chrome and do http://localhost:8080, and see the hello world.
Modules available for nodejs
The above example uses the http module. Here's a list of all of them in node 6: https://www.w3schools.com/nodejs/ref_modules.asp
Express
MVC framework built on Node. Has an express generator that will build the project dirs. From here:
Install the Express Generator by running the following from a terminal:
npm install -g express-generator
The -g switch installs the Express Generator globally on your machine so you can run it from anywhere. You won't want it as part of your project anyway.
We can now scaffold a new Express application called myExpressApp by running:
express myExpressApp
This creates a new folder called myExpressApp with the contents of your application. To install all of the application's dependencies (again shipped as NPM modules), go to the new folder and execute npm install:
cd myExpressApp npm install
At this point, we should test that our application runs. The generated Express application has a package.json file which includes a start script to run node ./bin/www. This will start the Node.js application running.
From a terminal in the Express application folder, run:
npm start
The Node.js web server will start and you can browse to http://localhost:3000 to see the running application.
Colours in console
npm install chalk
and
const chalk = require('chalk'); console.log(chalk.blue('Hello world!'));
Unit Testing
Mocha seems to have the lead...
Jasmine is used for BDD
- Jasmine quick start – excellent - http://www.bradoncode.com/blog/2015/05/12/angularjs-testing-getting-started/
My NoddyJasmine.htm, which you just open in Chrome, is:
<html> <head> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine-html.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/boot.min.js"></script> </head> <body> </body> <script type="text/javascript"> // Paste in the test code here. // This example from // http://www.bradoncode.com/blog/2015/05/12/angularjs-testing-getting-started/ // The describe function represents a spec (logical grouping of tests). describe('calculator', function () { // The it function indicates a test within the logical grouping. // The first bit is the displayed test name, the function is the actual test it('1 + 1 should equal 2, others', function() { expect(1 + 1).toBe(2); // Other possible tests using Jasmine's expect() method expect(true).toBe(true); expect(false).not.toBe(true); expect(1).toEqual(1); expect('foo').toEqual('foo'); expect('foo').not.toEqual('bar'); }); }); </script> </html>
SuperTest for calling APIs
- Extracted from http://www.marcusoft.net/2014/02/mnb-supertest.html
GET
The webservice, in getApp.js:
var app = require("express")(); app.get('/user', function(req, res){ res.send(200, { name: 'marcus' }); }); // In order to reach the app from other modules // we need to export the express application module.exports.getApp = app;
The test in getAppTest.js:
var request = require('supertest'); // Here we get hold of the express application // by using the exported 'getApp'-property var app = require("./getApp").getApp; describe('GET /users', function(){ it('respond with json', function(done){ // the request-object is the supertest top level api request(app) .get('/user') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); // note that we're passing the done as parameter to the expect }); });
POST
Post endpoint in postApp.js:
var express = require("express"); var app = express(); app.use(express.bodyParser()); app.post("/users", function(req, res){ var name = req.body.username; var email = req.body.email; // store it res.send(200, name + " is stored"); }); app.listen(3000);
The test - postAppTest.js:
var request = require('supertest'); describe("Posting is easy to test with supertest", function (){ it("posts a new user to /users", function(done){ var user = { username : 'marcus', email : 'marcus@marcus.com'}; request("http://localhost:3000") .post("/users") .send(user) .expect(200) .expect("marcus is stored", done); }); });
Mocking
Use Sinon instead of Mockito. There’s a before() and after() function. Stub the xhtmlrequest calls.
Database access
Connect to MySQL
Install the driver:
npm install -g mysql
Need:
// get the module for the driver var mysql = require('mysql'); // Create the connection var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "yourusername", password: "yourpassword" }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); });
Run this file using:
C:\Users\Your Name>node demo_db_connection.js
Query the db with an SQL string:
con.connect(function(err) { if (err) throw err; console.log("Connected!"); con.query(sql, function (err, result) { if (err) throw err; console.log("Result: " + result); }); });
All rows: ( https://www.w3schools.com/nodejs/nodejs_mysql_select.asp )
var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "yourusername", password: "yourpassword", database: "mydb" }); con.connect(function(err) { if (err) throw err; con.query("SELECT * FROM customers", function (err, result, fields) { if (err) throw err; console.log(result); }); });
Connect to Postgres
Need the pg driver.
'use strict' const pg = require('pg') const conString = 'postgres://username:password@localhost/node_hero' // make sure to match your own database's credentials pg.connect(conString, function (err, client, done) { if (err) { return console.error('error fetching client from pool', err) } client.query('SELECT $1::varchar AS my_first_query', ['node hero'], function (err, result) { done() if (err) { return console.error('error happened during query', err) } console.log(result.rows[0]) process.exit(0) }) })
client.query can run any SQL, INSERT, etc.
Connect to Mongo
Most use Mongoose library.
Get a free MongoDB database at https://www.mongodb.com/
Get the driver:
npm install mongodb