U
U
ura2rist2020-01-13 23:25:52
Node.js
ura2rist, 2020-01-13 23:25:52

How to include a file in a node?

I connect files, I start an index, the error takes off

internal/modules/cjs/loader.js:800
    throw err;
    ^

Error: Cannot find module './database'
Require stack:
- /home/admin/web/93.170.130.182/public_html/models/book.js
- /home/admin/web/93.170.130.182/public_html/app.js
- /home/admin/web/93.170.130.182/public_html/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
    at Function.Module._load (internal/modules/cjs/loader.js:690:27)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/admin/web/93.170.130.182/public_html/models/book.js:1:18)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Module.require (internal/modules/cjs/loader.js:852:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/admin/web/93.170.130.182/public_html/models/book.js',
    '/home/admin/web/93.170.130.182/public_html/app.js',
    '/home/admin/web/93.170.130.182/public_html/index.js'
  ]
}

app file content
const express = require('express'),
  app = express(),
  book = require('./models/book'),
  bodyParser = require('body-parser');

app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));

const test = ['Царство','Забытый мир','День'];

app.get('/',(request,response)=>{
  response.render('index',{
    test: test
  })
});

app.get('/create',(request, response)=>{
  response.render('create')
});

app.post('/create',(request,response)=>{
  book.test.create({
    name: 'текст'
  })
});

module.exports = app;

index
const app = require('./app'),
  config = require('./config'),
  book = require('./models/book'),
  database = require('./database');

app.listen(config.PORT,(err)=>{
  if(err){
    console.log(err);
  }
  console.log('Сервер работает');
})

book
const database = require('./database'),
  book = database.sequelize.define('book',{
    id: {
      type: database.Sequelize.INTEGER,
      autoIncrement: true,
      primaryKey: true,
      allowNull: false
    },
    name: {
      type: database.Sequelize.STRING,
      allowNull: false
    },
    author: {
      type: database.Sequelize.STRING,
      allowNull: false
    },
    description: {
      type: database.Sequelize.TEXT,
      allowNull: false
    },
    keyword: {
      type: database.Sequelize.STRING,
      allowNull: false
    },
    publishing: {
      type: database.Sequelize.STRING,
      allowNull: false
    },
    year:{
      type: database.Sequelize.STRING,
      allowNull: false
    },
    genre:{
      type: database.Sequelize.STRING,
      allowNull: false
    },
    img: {
      type: database.Sequelize.STRING,
      allowNull: false
    }
  }),
  author = database.sequelize.define('author',{
    id: {
      type: database.Sequelize.INTEGER,
      autoIncrement: true,
      primaryKey: true,
      allowNull: false
    },
    name: {
      type: database.Sequelize.STRING,
      allowNull: false
    }
  }),
  publishing = database.sequelize.define('publishing', {
    id: {
      type: database.Sequelize.INTEGER,
      autoIncrement: true,
      primaryKey: true,
      allowNull: false
    },
    name: {
      type: database.Sequelize.STRING,
      allowNull: false
    }
  }),
  genre = database.Sequelize.define('genre',{
    id: {
      type: database.Sequelize.INTEGER,
      autoIncrement: true,
      primaryKey: true,
      allowNull: false
    },
    name: {
      type: database.Sequelize.STRING,
      allowNull: false
    }
  }),
  test = database.Sequelize.define('test',{
    name: {
      type: database.Sequelize.STRING,
      allowNull: false
    }
  });

module.exports = {
  book: book,
  author: author,
  publishing: publishing,
  test: test
}

database
const config = require('./config'),
  Sequelize = require('sequelize'),
  sequelize = new Sequelize(config.DB_NAME, config.DB_USER, config.DB_PASSWORD,{
    dialect: 'mysql',
    host: config.DB_URL,
    define:{
      freezeTableName: true																				//Уникальное имя для таблицы, для отключения даты и время timestamps: false
    }
  });

module.exports = {
  sequelize: sequelize,
  Sequelize: Sequelize
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question