Questions

Q: Where do I start?

I do not want to use a framework. I want, however, to use npm to start building the app.

I am going to start with testing if I can talk to the MongoDB database.

Resources for this question.


In [2]:
!npm install mongodb --save


+ npm install mongodb --save
grilled_flpd_data@1.0.0 /home/dmmmd/Dropbox/Wyncode-Academy/web-presence/zipped-code/content/posts/python-mongodb/grilled_flpd_data
└── mongodb@2.2.10 

Check on this friendly warning.

npm WARN enoent ENOENT: no such file or directory, open '/home/dmmmd/Dropbox/Wyncode-Academy/web-presence/zipped-code/content/posts/python-mongodb/grilled_flpd_data/package.json'


In [2]:
!ls -als


+ ls -als
total 108
 8 drwxrwxr-x  6 dmmmd dmmmd  4096 Sep 16 23:14 .
 8 drwxr-xr-x  5 dmmmd dmmmd  4096 Sep 16 12:25 ..
 8 drwxrwxr-x  7 dmmmd dmmmd  4096 Sep 16 13:44 .git
 8 -rw-rw-r--  1 dmmmd dmmmd    34 Sep 16 13:44 .gitignore
16 -rw-rw-r--  1 dmmmd dmmmd 11354 Sep 16 15:09 grilled_flpd_data_01.ipynb
 8 -rw-rw-r--  1 dmmmd dmmmd   699 Sep 16 23:11 grilled_flpd_data_01.ipynb-meta
 8 -rw-rw-r--  1 dmmmd dmmmd    72 Sep 16 23:13 grilled_flpd_data_02.ipynb
 8 -rw-rw-r--  1 dmmmd dmmmd   699 Sep 16 23:12 grilled_flpd_data_02.ipynb-meta
 8 drwxr-xr-x  2 dmmmd dmmmd  4096 Sep 16 23:13 .ipynb_checkpoints
 8 drwxrwxr-x  2 dmmmd dmmmd  4096 Sep 16 14:01 local_db
 8 drwxrwxr-x 18 dmmmd dmmmd  4096 Sep 16 23:14 node_modules
 8 -rw-rw-r--  1 dmmmd dmmmd  1024 Sep 16 23:14 package.json
 4 -rw-rw-r--  1 dmmmd dmmmd     0 Sep 16 23:13 Untitled.txt

I forgot npm init. That is why I have no package.json file.


In [ ]:
!npm init


+ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (grilled_flpd_data) 

I have to run npm init inside of a terminal and not a Jupyter notebook. Some Jupyter notebooks don't do stdin it seems. NB: I am working in a Jupyter notebook with a NodeJS kernel.

And now a package.json file exists.


In [4]:
!cat package.json


+ cat package.json
{
  "name": "grilled_flpd_data",
  "version": "1.0.0",
  "description": "Diplay data from a data collection based on existing collections in MongoDB from the City of Fort Lauderdale Police Department traffic citations and accidents data obtained at the Code for Fort Lauderdale Hackathon 2016. This is an exercise in building a website using libraries as much as possible and frameworks as little as possible. Grilled is a ReactJS component that is the object of this exercise.",
  "main": "index.js",
  "dependencies": {
    "mongodb": "2.2.10"
  },
  "devDependencies": {},
  "scripts": {
    "test": "grunt test"
  },
  "repository": {
    "type": "git",
    "url": "git+ssh://git@github.com/dm-wyncode/grilled-flpd-data.git"
  },
  "keywords": [
    "reactjs",
    "grilled",
    "javascript",
    "mongodb"
  ],
  "author": "Don Morehouse",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/dm-wyncode/grilled-flpd-data/issues"
  },
  "homepage": "https://github.com/dm-wyncode/grilled-flpd-data#readme"
}

Add code to connect to the server and the database 'app'


In [5]:
const MongoClient = require('mongodb').MongoClient,
      assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017/app';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db){
   assert.equal(null, err);
   console.log("Connected successfully to server.");
   db.close()
});


Connected successfully to server.

So far so good!

Side note: jshint in Vim is not honoring ECMA6.

I followed the instructions in the blog and now Vim lints *.js files as expected!

End side note: jshint in Vim is not honoring ECMA6.

Question: How can I reuse that database code if I want to check my database connection?

Answer: Make an npm package.

mkdir local_db
cd local_db
npm init
vim index.js

In [6]:
!tree local_db


+ tree local_db
local_db
├── index.js
└── package.json

0 directories, 2 files

In [7]:
!cat local_db/index.js


+ cat local_db/index.js
const testDBConnection = function testDBConnection() {
  const MongoClient = require('mongodb').MongoClient,
        assert = require('assert'),
        // Connection URL
        url = 'mongodb://localhost:27017/app',
        message = "Connected successfully to MongoDB server.";
  // Use connect method to connect to the server
  MongoClient.connect(url, function(err, db){
     assert.equal(null, err);
     db.close();
  });
  return message 
};

module.exports = testDBConnection

Trying out the new package


In [1]:
const testDatabase = require('./local_db');
console.log(testDatabase());


Connected successfully to MongoDB server.

Success! A simple module was created and imported and run.

Resources