0.1.1 • Published 4 years ago
node-bp v0.1.1
Generate a boilerplate with NodeJS
This is a practice run at building a NPM package to boilerplate a quick start build of a Nodejs API server it is not meant to be used by the public at this time
Start a project
cd into the project
npm init
Install dependencies
npm install express knex sqlite3 node-bp
Install dev dependencies
npm install nodemon --only=dev
Install the files
run the command create
in your console
Update your scripts by adding
"start": "node index.js"
this will run the server though you will have to stop and start it after an update to a file.
&&
"server": "nodemon index.js" this will watch for changes and update the server as you go
Setup the database
run the command in the console
knex init
the above command will make a knexfile for your project
configure the knexfile
module.exports = {
development: {
client: 'sqlite3',
connection: {
filename: './users.db3'
},
useNullAsDefault: true,
},
staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},
production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}
};
after configuring the knexfile you need to follow the next commands
knex migrate:make <name of your database>
open the migrations folder and the file and this simply code
exports.up = function(knex) {
return knex.schema
.createTable('users', users => {
users
.increments();
users
.string('username', 128)
.notNullable()
.unique();
users
.string('password', 128)
.notNullable();
users
.string('firstname', 128)
.notNullable();
users
.string('lastname', 128)
.notNullable();
users
.string('details', 128)
})
};
exports.down = function(knex) {
return knex.schema
.dropTableIfExists('users')
};
after pasting the above code in the migration file run the following command
knex migrate:latest
Start the server
npm start
or
npm run server