JS-301-NodeServer
  • Introduction
  • js_library
    • Node Server
      • 00 - Intro
        • 01 - Purpose
        • 02 - Back-End Setup
        • 03 - Terms Cheat Sheet
      • 01 - Server Set up
        • 01 - npm packages
        • 02 - Express Intro
        • 03 - Express code
      • 02 - Development Tools
        • 01 - Nodemon Intro
        • 02 - Postman Intro
        • 03 - Postman set up
      • 03 - Routes Intro
        • 01 - Routes intro
        • 02 - Express Router() intro
        • 03 - Challenge 1
        • 04 - Challenge 2
      • 04 - Database Intro
        • 00 - DB Intro and Set up
          • 00 - DB Intro
          • 01 - PostgreSQL Intro
          • 02 - Install
        • 01 - Sequelize Intro
          • 01 - Sequelize intro
          • 02 - Initialize
      • 05 - Model View Controller
        • 01 - MVC
          • 00 - MVC Intro
        • 02 - Models
          • 01 - Intro to Models
          • 02 - Test Model
        • 03 - Controllers
          • 00 - Controllers Intro
          • 01 - Controller Set up
          • 02 - Create Method
          • 03 - req.body()
          • 04 - Crafting the Response
          • 05 - Sending the Response
          • 06 - JSON Response
          • 07 - Error Handling
        • 04 - Conclusion
      • 06 - Tokenization
        • 01 - JWT Intro
          • 01 - JWT intro
        • 02 - User Create
          • 01 - User Create
          • 02 - Refactor
        • 03 - User Token
          • 01 - JWT Package
          • 02 - Adding JWT
          • 03 - ENV
      • 07 - Encryption
        • 01 - bcrypt
        • 02 - bcrypt setup
      • 08 - Session
        • 00 - Session Intro
        • 01 - Sign In Method
        • 02 - Sign In Bcrypt
        • 03 - Sign In JWT
      • 09 - Middleware
        • 01 - Test Client HTML
        • 02 - Test Client JS
        • 03 - Middleware intro
        • 04 - Headers intro
        • 05 - Server Update
        • 06 - Test Post
        • 07 - Test Post Refactor
        • 08 - Post Data
        • 09 - Fetch From One
      • 10 - Authenticated Routes
        • 01 - Intro to Authenticated Routes
        • 02 - Validate Session
        • 03 - Changes to app.js
        • 04 - authtestcontroller.js
        • 05 - Delete an Item
        • 06 - Update an Item
        • 07 - Postman Testing
      • 11 - Authenticated Requests
        • 00 - Additions to index
        • 01 - Anatomy of a Request
        • 02 - Create User
        • 03 - Getting a Token
        • 04 - Get Items From One User
        • 05 - Creating an Item for a User
        • 06 - Get one item
        • 07 - Update an Item
        • 08 - Deleting an Item
        • 09 - Deleting with a Custom Event
      • 12 - Workout Log Server
        • 00 - Intro
      • 13 - More Sequelize Functions
        • Migrations
          • 00 - Intro
          • 01 - init and config
          • 02 - Creating the First Migration
          • 03 - Running Migrations
          • 04 - Reverting Migrations
          • 05 - Seeds
          • 06 - Reverting Seeds
        • Queries
          • 00 - Intro
          • 01 - Queries
Powered by GitBook
On this page
  • User Model
  • Analysis
  • app.js
  • Analysis
  • usercontroller.js
  • Analysis
  • Postman
  • Postgres
  1. js_library
  2. Node Server
  3. 06 - Tokenization
  4. 02 - User Create

01 - User Create

In this module, we'll start to set up the necessary items for creating a new user.

So far, we've just been putting data into a table in our database. However, if someone were to look at that data, there's no way to tell who actually put the data there. Additionally, there's no security protocols in place, so anyone can connect to the database and modify its contents in whatever way they choose. We can use JWT to fix both of these issues. We have a little bit of prep to do first, though: We'll use Sequelize and create a User model to create a new user in the database.

User Model

Let's set up a new user model. Create a file user.js in the models folder.

module.exports = function (sequelize, DataTypes) {
           //1      //2
    return sequelize.define('user', {
        username: DataTypes.STRING, //3
        passwordhash: DataTypes.STRING //3
    });
};

Analysis

This should look familiar:

1. A function with a Sequelize object that calls the define method.

2. A first parameter that will create a users table in Postgres.

3. An object with username and passwordhash that will be the columns in the table. We'll talk more about a passwordhash later.

app.js

We'll need to set up a route to the user controller methods in app.js. We have added the entire file for orientation:

var express = require('express');
var app = express();
var test= require('./controllers/testcontroller')
var user = require('./controllers/usercontroller') //1
var sequelize = require('./db');
var bodyParser = require('body-parser');

sequelize.sync(); // tip: {force: true} for resetting tables

app.use(bodyParser.json());

app.use('/test', test);

app.use('/api/user', user); //2

//3 You could also write it this way without the require statement above.
//app.use('/api/user', require('./controllers/usercontrollers'));

app.listen(3000, function(){
    console.log('App is listening on 3000.')
});

Analysis

  1. We import the usercontroller.js file.

  2. We set up a route to the endpoints for the api/user route.

  3. Just another way to write out your routes. Just be consistent.

usercontroller.js

If you haven't already, create a new file inside the controllers folder and call it usercontroler.js. We'll need to add some code to that file. Note: if Lebowski isn't your style, enter in your own flavor for the string values:

var express = require('express')
var router = express.Router()      //1
var sequelize = require('../db');
var User = sequelize.import('../models/user'); 

/*************************
** Create User Endpoint: Starter***
**************************/
//2
router.post('/createuser', function (req, res) {

  var username = "The Dude";
  var pass = "therugtiedtheroomtogether";               /**3**/

  User.create({
    username: username,
    passwordhash: pass

  }).then(
    function message(){
      res.send("I hate The Eagles, man");
    }
  );
})

module.exports = router;

Analysis

This should look familiar again:

1. We bring in our necessary imports. Same as the testcontroller, just with a User model now.

2. We start out our POST method for a createuser endpoint.

3. Inside the method, we have the basics for creating a new user and returning a message in the response.

Postman

Let's quickly test this with Postman.

1. Start your server then open Postman.

2. Figure out the endpoint to send a POST request to.

3. Press send.

4. You should see the response string:

Postgres

You should also go check Postgres to see that the data showed up:

Previous02 - User CreateNext02 - Refactor

Last updated 7 years ago

screenshot
screenshot