Difference between revisions of "User:Roger Pearse/JavaScript Basics"
Roger Pearse (talk | contribs) |
Roger Pearse (talk | contribs) |
||
Line 90: | Line 90: | ||
= Express = | = Express = | ||
− | MVC framework built on Node. Has an express generator that will build the project dirs. | + | MVC framework built on Node. Has an express generator that will build the project dirs. From [https://code.visualstudio.com/docs/nodejs/nodejs-tutorial here]: |
− | .... | + | Install the Express Generator by running the following from a terminal: |
+ | |||
+ | <pre> | ||
+ | npm install -g express-generator | ||
+ | </pre> | ||
+ | |||
+ | 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: | ||
+ | <pre> | ||
+ | express myExpressApp | ||
+ | </pre> | ||
+ | |||
+ | 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: | ||
+ | <pre> | ||
+ | cd myExpressApp | ||
+ | npm install | ||
+ | </pre> | ||
+ | |||
+ | 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: | ||
+ | <pre> | ||
+ | npm start | ||
+ | </pre> | ||
+ | |||
+ | The Node.js web server will start and you can browse to http://localhost:3000 to see the running application. | ||
= Unit Testing = | = Unit Testing = |
Revision as of 13:59, 4 July 2017
Contents
Installing stuff with NPM
Starting a new node project
Start any node.js project by:
mkdir MyProject cd MyProject echo {} >> package.json
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 configuration.
What’s –save-dev? That’s an option that updates 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 applicaiton i.e. in production.
http://www.bradoncode.com/blog/2015/05/19/karma-angularjs-testing/
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
- Great little tutorial! – add to basics - VS Code is the editor to use.
Node.js
My first js (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");
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.
Unit Testing
Mocha seems to have the lead...
Jasmine = BDD
- Jasmine quick start – excellent - http://www.bradoncode.com/blog/2015/05/12/angularjs-testing-getting-started/
Mocking
Use Sinon instead of Mockito. There’s a before() and after() function. Stub the xhtmlrequest calls.