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 项目地址: 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 项目地址: https://gitcode.com/gh_mirrors/ne/next-postgres-sequelize