Next.js + Postgres + Sequelize 项目教程

Next.js + Postgres + Sequelize 项目教程

next-postgres-sequelize [OUTDATED] React 16.8.4 + NextJS 8.0.3 + Emotion + Sequelize 5/Postgres + Passport Local Auth + Google App Engine or Heroku Deployment next-postgres-sequelize 项目地址: https://gitcode.com/gh_mirrors/ne/next-postgres-sequelize

1. 项目目录结构及介绍

next-postgres-sequelize/
├── components/
│   ├── Header.js
│   ├── Footer.js
│   └── ...
├── pages/
│   ├── index.js
│   ├── about.js
│   └── ...
├── public/
│   ├── images/
│   ├── styles/
│   └── ...
├── styles/
│   ├── globals.css
│   ├── Home.module.css
│   └── ...
├── utils/
│   ├── db.js
│   ├── auth.js
│   └── ...
├── .env
├── .gitignore
├── next.config.js
├── package.json
└── README.md

目录结构说明

  • components/: 存放React组件,如Header、Footer等。
  • pages/: 存放Next.js页面组件,每个文件对应一个路由。
  • public/: 存放静态资源,如图片、样式文件等。
  • styles/: 存放CSS样式文件,支持模块化CSS。
  • utils/: 存放工具函数和数据库连接配置文件。
  • .env: 环境变量配置文件。
  • .gitignore: Git忽略文件配置。
  • next.config.js: Next.js配置文件。
  • package.json: 项目依赖和脚本配置文件。
  • README.md: 项目说明文档。

2. 项目启动文件介绍

package.json

{
  "name": "next-postgres-sequelize",
  "version": "1.0.0",
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "^10.0.0",
    "react": "^17.0.0",
    "react-dom": "^17.0.0",
    "sequelize": "^6.0.0",
    "pg": "^8.0.0"
  }
}

启动命令

  • 开发模式: npm run dev
  • 构建项目: npm run build
  • 启动生产服务器: npm start

3. 项目配置文件介绍

.env

DATABASE_URL=postgres://username:password@localhost:5432/dbname
JWT_SECRET=your_jwt_secret

next.config.js

module.exports = {
  env: {
    DATABASE_URL: process.env.DATABASE_URL,
    JWT_SECRET: process.env.JWT_SECRET
  },
  webpack: (config, { isServer }) => {
    if (!isServer) {
      config.node = {
        fs: 'empty'
      }
    }
    return config
  }
}

utils/db.js

const { Sequelize } = require('sequelize');

const sequelize = new Sequelize(process.env.DATABASE_URL, {
  dialect: 'postgres',
  protocol: 'postgres',
  dialectOptions: {
    ssl: {
      require: true,
      rejectUnauthorized: false
    }
  }
});

module.exports = sequelize;

配置文件说明

  • .env: 存储敏感信息和环境变量,如数据库连接字符串和JWT密钥。
  • next.config.js: Next.js的配置文件,用于设置环境变量和Webpack配置。
  • utils/db.js: 数据库连接配置文件,使用Sequelize连接到PostgreSQL数据库。

next-postgres-sequelize [OUTDATED] React 16.8.4 + NextJS 8.0.3 + Emotion + Sequelize 5/Postgres + Passport Local Auth + Google App Engine or Heroku Deployment next-postgres-sequelize 项目地址: https://gitcode.com/gh_mirrors/ne/next-postgres-sequelize

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌爱芝Sherard

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值