Code Monkey home page Code Monkey logo

sails-test's Introduction

如何启动

  1. cd sails-test && npm install
  2. sails lift 或者 nodeman app.js

参考文档 1-2视频 3-4视频

sails

  1. 入门地址
npm install sails -g
sails new test-project
cd test-project
sails lift(会有提示,1. FOR DEV:alter)
  1. 新建一个restful的API
sails genenrate api hello

至此,你目前可以在磁盘缓存数据的基础上调用hello这个api接口 新增用户=>PUSH http://localhost:1337/hello/Create?userId=13&userName=san 修改用户=>PATCH http://localhost:1337/hello/1?userId=11&userName=wu 删除用户=>DEL http://localhost:1337/hello?id=1 (id为创建后返回的id) 查找所有用户=>GET http://localhost:1337/hello 通过主键查找一个用户=>GET http://localhost:1337/hello/1

  1. 使用mongoDB存储
  • 首先你需要安装一个mongoDB,这里我自己创建了一个数据库 hello

  • 安装 sails-mysql : npm install -g sails-mysql

  • 修改models/Hello.js 配置你的表以及字段的属性

     ```
     
     module.exports = {
     
       tableName: 'hello',
       primaryKey: 'id',
     
       attributes: {
         userId: {
           type: 'string',
           required: true,
           unique: true,
           columnName: 'user_id',
         },
         userName: {
           type: 'string',
           columnName: 'user_name',
           defaultsTo: '',
         },
       },
     
       async beforeCreate(data, next) {
         return await next()
       },
     
       async afterCreate(data, next) {
         return await next()
       },
     
     };
     
     
     ```
    
    • config中的datastores.js里面配置mysql的用户名/密码/IP/端口号/数据库

      module.exports.datastores = {
        default: {
          adapter: 'sails-mysql',
          url: 'mysql://root:password@localhost:3306/hello',
        },
      };
      
  1. 至此,你可以重启项目,调用你的接口,数据库里面就能看到了

ps: 你可以全局安装nodeman, nodeman app.js 能帮你监控app.js是否改动,改动则更新配置

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.