This is the current project stage before pushing to GitHub.
Project path:
/Users/anbschool0018/Desktop/Project/00.SCHOOL/00.SchoolProject/Repo/test
Date recorded:
June 1, 2026
The current learning goal is:
Build the project step by step like a beginner.
Do not jump ahead.
Understand each file before creating the next one.
So far, only these parts are finished:
1. Basic Express server in app.js
2. Neon PostgreSQL connection config in config/database.js
The next learning step is:
Create models/Catalog.js
.
├── .dist/
├── .env
├── .gitignore
├── app.js
├── config/
│ └── database.js
├── controllers/
├── models/
├── node_modules/
├── package-lock.json
├── package.json
├── readme.md
├── routes/
└── views/
Important:
controllers/
models/
routes/
views/
exist, but they are still empty at this checkpoint.
This is the database design we want to build later:
catalog [] {
catalog_id int pk
catalog_name varchar
create_at timestamp
}
unit [] {
unit_id int pk
unit_name varchar
create_at timestamp
}
products [] {
catalog_id int fk
product_id int pk
product_name varchar
product_price int
unit_id int fk
product_quantity int
create_at timestamp
}
catalog.catalog_id < products.catalog_id
unit.unit_id < products.unit_id
We have not created these models yet.
The next model to create is:
models/Catalog.js
File:
app.js
Current code:
import 'dotenv/config';
import express from 'express';
import sequelize from './config/database.js';
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.get('/', function(req, res) {
res.send('Express server is running');
});
sequelize
.authenticate()
.then(function() {
app.listen(port, function() {
console.log(`Server running on http://localhost:${port}`);
console.log('Database connected');
});
})
.catch(function(error) {
console.error('Unable to connect to the database:', error);
process.exit(1);
});Line-by-line meaning:
import 'dotenv/config';Loads the .env file.
This must come before using process.env.DATABASE_URL.
import express from 'express';Imports Express, the web server framework.
import sequelize from './config/database.js';Imports the Neon database connection from config/database.js.
const app = express();Creates the Express app.
const port = process.env.PORT || 3000;Uses PORT from .env.
If PORT does not exist, it uses 3000.
app.use(express.json());Allows Express to read JSON request bodies.
Example body:
{
"catalog_name": "Drink"
}app.use(express.urlencoded({ extended: false }));Allows Express to read form data.
app.get('/', function(req, res) {
res.send('Express server is running');
});Creates the homepage route.
When opening:
http://localhost:3000
The browser should show:
Express server is running
sequelize
.authenticate()Tests the Neon PostgreSQL connection.
.then(function() {Runs only if the database connection works.
app.listen(port, function() {Starts the Express server.
Important:
The server starts only after Neon connects successfully.
console.log(`Server running on http://localhost:${port}`);
console.log('Database connected');Prints success messages in the terminal.
.catch(function(error) {Runs if the database connection fails.
console.error('Unable to connect to the database:', error);Prints the database error.
process.exit(1);Stops the app if the database cannot connect.
File:
config/database.js
Current code:
import { Sequelize } from 'sequelize';
if (!process.env.DATABASE_URL) {
throw new Error('DATABASE_URL is missing from .env');
}
const sequelize = new Sequelize(process.env.DATABASE_URL, {
dialect: 'postgres',
logging: false,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},
});
export default sequelize;Line-by-line meaning:
import { Sequelize } from 'sequelize';Imports Sequelize.
Sequelize is the tool that lets JavaScript talk to PostgreSQL.
if (!process.env.DATABASE_URL) {Checks if the Neon connection link is missing.
throw new Error('DATABASE_URL is missing from .env');Stops the app with a clear error if .env is not set correctly.
const sequelize = new Sequelize(process.env.DATABASE_URL, {Creates the database connection using the Neon URL from .env.
dialect: 'postgres',Tells Sequelize the database type is PostgreSQL.
logging: false,Stops Sequelize from printing every SQL query.
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},Enables SSL.
Neon needs SSL to connect.
export default sequelize;Exports the database connection so other files can use it.
The .env file exists and contains:
DATABASE_URL
PORT
The real values are not written here because .env contains secrets.
Expected shape:
DATABASE_URL=postgresql://username:password@host.neon.tech/database_name?sslmode=require
PORT=3000Important:
.env should not be pushed to GitHub.
.gitignore already ignores .env.
First, make sure no old server is using port 3000:
lsof -i :3000If something is using port 3000, kill it:
kill PID_NUMBERThen start the app:
npm startExpected result:
Server running on http://localhost:3000
Database connected
Then open:
http://localhost:3000
Expected browser text:
Express server is running
Problem:
Failed to lookup view "error"
Meaning:
An older version of the server was still running.
It was trying to render views/error.ejs.
Fix:
lsof -i :3000
kill PID_NUMBERThen run:
npm startNext file to create:
models/Catalog.js
Why:
The first table in the ERD is:
catalog
So the first Sequelize model should be:
Catalog
Code to create next:
import { DataTypes } from 'sequelize';
import sequelize from '../config/database.js';
const Catalog = sequelize.define(
'Catalog',
{
catalog_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
catalog_name: {
type: DataTypes.STRING,
allowNull: false,
},
create_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
},
{
tableName: 'catalog',
timestamps: false,
}
);
export default Catalog;After creating it, check syntax:
node --check models/Catalog.jsIf there is no output, the syntax is correct.
Continue in this order:
1. models/Catalog.js
2. models/Unit.js
3. models/Product.js
4. models/index.js
5. controllers/catalogsController.js
6. controllers/unitsController.js
7. controllers/productsController.js
8. routes/catalogs.js
9. routes/units.js
10. routes/products.js
11. connect routes in app.js
12. run and test with curl
Do not skip ahead.
Build one file, understand it, test it, then move to the next file.