GithubHelp home page GithubHelp logo

be-bp-nodejs's Introduction

How to Build a Node.js Server Using an SQLite Database

Creating a Git Repository & Adding Dependencies

Create a directory for your server to live

  • mkdir < server-directory-name >
  • git init
  • npx gitignore node //creates git.ignore for node
  • npm init -y //creates package.json

Add your dependencies

  • npm i express helmet cors knex sqlite3 bcryptjs* jsonwebtoken*
  • npm i nodemon -D

* only if using database with login/register

  • Update package.json scripts to include server (and start) script(s):
"scripts": {
	"server": "nodemon",  //defaults to index.js
	"start": "node index.js" (optional, for production)
}

Adding Migrations & Seeding

  • npx knex init //creates an abstracted knexfile.js

  • Update knexfile.js to:

module.exports = {
  development: {
    client: 'sqlite3',
    useNullAsDefault: true,
    connection: {
      filename: './data/auth.db3',
    },
    pool: {
      afterCreate: (conn, done) => {
        conn.run('PRAGMA foreign_keys = ON', done);
      },
    },
    migrations: {
      directory: './data/migrations',
    },
    seeds: {
      directory: './data/seeds',
    },
  },
};

Create a table within a migration folder:

  • npx knex migrate:make table_name

  • Example schema for a user table:

exports.up = function(knex) {
    return knex.schema.createTable('users', users => {
      users.increments();

      users
        .string('username', 128)
        .notNullable()
        .unique();

      users
        .string('password', 128)
        .notNullable();
    });
  };

  exports.down = function(knex) {
    return knex.schema.dropTableIfExists('users');
  };

  • npx knex migrate:latest (creates .db3 file)
  • npx knex migrate:rollback (deletes .db3 file)

Seeding (optional):

  • npx knex seed:make 001-seedName (makes a new seed)

  • Example seeds > 001-exampleSeed.js file:

exports.seed = function(knex, Promise) {
  // Deletes ALL existing entries
  return knex('table_name').truncate()
    .then(function () {
      // Inserts seed entries
      return knex('table_name').insert([
        { username: 'Amanda', password: '123' },
        { username: 'Herman', password: 'password'  },
        { username: 'Boo', password: 'hello'  }
      ]);
    });
};
  • npx knex seed:run (runs/resets seed)

Creating Server, Routes and Helper Functions

  • In the data directory, create a file called "dbConfig.js" and include the following code:
const knex = require('knex');

const knexConfig = require('../knexfile.js');

module.exports = knex(knexConfig.development);
  • Review the code in this repository for more information.

< -------- after forking and cloning this repo ---------->

  • run command npm install to get your dependencies listed above.
  • If you fork and clone this repo you do not need to follow README steps, they are already completed.

be-bp-nodejs's People

Contributors

amlane avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.