Code Monkey home page Code Monkey logo

express-example's Introduction

express-example

This repository demonstrates the usage of sequelize within an express application. The implemented logic is a simple task tracking tool.

Starting the app

npm install
npm start

This will start the application and create an sqlite database in your app dir. Just open http://localhost:3000.

The setup

In order to understand how this application has been built, you can find the executed steps in the following snippet. You should be able to adjust those steps according to your needs. Please note that the view and the routes aren't described. You can find those files in the repo.

mkdir express-example
cd express-example
npm install express-generator
node_modules/.bin/express -f
npm install
npm install --save [email protected] sequelize-cli sqlite3
node_modules/.bin/sequelize init
node_modules/.bin/sequelize model:create --name User --attributes username:string
node_modules/.bin/sequelize model:create --name Task --attributes title:string

You will now have a basic express application with some additional directories (config, models, migrations). Also you will find two migrations and models. One for the User and one for the Task.

In order to associate the models with each other, you need to change the models like this:

// task.js
// ...
classMethods: {
  associate: function(models) {
    Task.belongsTo(models.User);
  }
}
// ...
// user.js
// ...
classMethods: {
  associate: function(models) {
    User.hasMany(models.Task)
  }
}
// ...

If you want to use the automatic table creation that sequelize provides, you have to adjust the bin/www file to this:

#!/usr/bin/env node

var app = require('../app');
var debug = require('debug')('init:server');
var http = require('http');
var models = require("../models");

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

var server = http.createServer(app);

models.sequelize.sync().then(function () {
  server.listen(port);
  server.on('error', onError);
  server.on('listening', onListening);
});

function normalizePort(val) { /* ... */ }
function onError(error) { /* ... */ }
function onListening() { /* ... */ }

And finally you have to adjust the config/config.json to fit your environment. Once thats done, your database configuration is ready!

The tests

You can run the tests by executing npm test.

express-example's People

Contributors

sdepold avatar iblazevic avatar uwolfer avatar salsaysal avatar asoltys avatar designeng avatar stephaneerard avatar sammychl avatar

Watchers

James Cloos avatar Yinhuan Yuan avatar  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.