mirror of
https://github.com/SrIzan10/starters.git
synced 2026-05-01 11:05:16 +00:00
Express example (#45)
This commit is contained in:
9
examples/expressjs-postgres/.gitignore
vendored
9
examples/expressjs-postgres/.gitignore
vendored
@@ -19,3 +19,12 @@
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# local env files
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
# out dir
|
||||
dist/
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
FROM node:alpine
|
||||
|
||||
# Create app directory
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
# Install app dependencies
|
||||
# A wildcard is used to ensure both package.json AND package-lock.json are copied
|
||||
# where available (npm@5+)
|
||||
COPY package*.json ./
|
||||
|
||||
RUN npm install
|
||||
# If you are building your code for production
|
||||
# RUN npm ci --only=production
|
||||
|
||||
# Bundle app source
|
||||
COPY . .
|
||||
|
||||
CMD [ "npm", "run", "start" ]
|
||||
@@ -3,7 +3,7 @@
|
||||
This example starts an [ExpressJS](https://expressjs.com/) server that connects
|
||||
to a Railway PostgreSQL database.
|
||||
|
||||
[](https://railway.app/new?template=https%3A%2F%2Fgithub.com%2Frailwayapp%2Fexamples%2Ftree%2Fmaster%2Fexamples%2Fexpressjs-postgres)
|
||||
[](https://railway.app/new?template=https%3A%2F%2Fgithub.com%2Frailwayapp%2Fexamples%2Ftree%2Fmaster%2Fexamples%2Fexpressjs-postgres&plugins=postgresql)
|
||||
|
||||
## ✨ Features
|
||||
|
||||
@@ -12,6 +12,7 @@ to a Railway PostgreSQL database.
|
||||
|
||||
## 💁♀️ How to use
|
||||
|
||||
- Install dependencies `yarn`
|
||||
- [Create a Railway project with the Postgres plugin](https://dev.new)
|
||||
- Connect to your Railway project `railway init`
|
||||
- Start the server `railway run yarn start`
|
||||
|
||||
@@ -1,17 +1,26 @@
|
||||
{
|
||||
"name": "with-express",
|
||||
"name": "expressjs-postgres",
|
||||
"private": true,
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"author": "Jake Runzer",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"start": "node src/index.js",
|
||||
"build": "echo 'No build step'"
|
||||
"dev": "nodemon src/index.ts",
|
||||
"build": "tsc",
|
||||
"start": "node dist/index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": "^1.19.0",
|
||||
"express": "^4.17.1",
|
||||
"pg": "^8.5.1"
|
||||
},
|
||||
"devDependencies": {}
|
||||
"devDependencies": {
|
||||
"@types/body-parser": "^1.19.0",
|
||||
"@types/express": "^4.17.11",
|
||||
"@types/pg": "^7.14.9",
|
||||
"nodemon": "^2.0.7",
|
||||
"ts-node": "^9.1.1",
|
||||
"typescript": "^4.1.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
const express = require("express");
|
||||
const pg = require("pg");
|
||||
|
||||
// Connect to the database using the DATABASE_URL environment
|
||||
// variable injected by Railway
|
||||
const pool = new pg.Pool();
|
||||
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
app.get("/", async (req, res) => {
|
||||
const { rows } = await pool.query("SELECT NOW()");
|
||||
res.send(`Hello, World! ${rows[0].now}`);
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening at http://localhost:${port}`);
|
||||
});
|
||||
23
examples/expressjs-postgres/src/index.ts
Normal file
23
examples/expressjs-postgres/src/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import bodyParser from "body-parser";
|
||||
import express from "express";
|
||||
import pg from "pg";
|
||||
|
||||
// Connect to the database using the DATABASE_URL environment
|
||||
// variable injected by Railway
|
||||
const pool = new pg.Pool();
|
||||
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3333;
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.raw({ type: "application/vnd.custom-type" }));
|
||||
app.use(bodyParser.text({ type: "text/html" }));
|
||||
|
||||
app.get("/", async (req, res) => {
|
||||
const { rows } = await pool.query("SELECT NOW()");
|
||||
res.send(`Hello, World! The time from the DB is ${rows[0].now}`);
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening at http://localhost:${port}`);
|
||||
});
|
||||
13
examples/expressjs-postgres/tsconfig.json
Normal file
13
examples/expressjs-postgres/tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
|
||||
"incremental": true,
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -13,6 +13,7 @@ This is an [ExpressJS](https://expressjs.com/) REST API that uses [Prisma](https
|
||||
|
||||
## 💁♀️ How to use
|
||||
|
||||
- Install dependencies `yarn`
|
||||
- [Provision a Postgres container on Railway](https://dev.new)
|
||||
- Connect to your Railway project with `railway init`
|
||||
- Migrate the database `railway run yarn migrate:dev`
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import express from "express";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import bodyParser from "body-parser";
|
||||
import express from "express";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
|
||||
30
examples/expressjs/.gitignore
vendored
Normal file
30
examples/expressjs/.gitignore
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
*.pem
|
||||
|
||||
# debug
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# local env files
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
# out dir
|
||||
dist/
|
||||
22
examples/expressjs/README.md
Normal file
22
examples/expressjs/README.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# ExpressJS Example
|
||||
|
||||
This example starts an [ExpressJS](https://expressjs.com/) server that connects
|
||||
to a Railway PostgreSQL database.
|
||||
|
||||
[](https://railway.app/new?template=https%3A%2F%2Fgithub.com%2Frailwayapp%2Fexamples%2Ftree%2Fmaster%2Fexamples%2Fexpressjs)
|
||||
|
||||
## ✨ Features
|
||||
|
||||
- Postgres
|
||||
- Express
|
||||
|
||||
## 💁♀️ How to use
|
||||
|
||||
- Install dependencies `yarn`
|
||||
- Connect to your Railway project `railway init`
|
||||
- Start the server `railway run yarn start`
|
||||
|
||||
## 📝 Notes
|
||||
|
||||
The server started simply returns the current time in the database. The SQL
|
||||
query is located in `src/index.js`.
|
||||
25
examples/expressjs/package.json
Normal file
25
examples/expressjs/package.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "expressjs",
|
||||
"private": true,
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"author": "Jake Runzer",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"dev": "nodemon src/index.ts",
|
||||
"build": "tsc",
|
||||
"start": "node dist/index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": "^1.19.0",
|
||||
"express": "^4.17.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/body-parser": "^1.19.0",
|
||||
"@types/express": "^4.17.11",
|
||||
"@types/node": "^14.14.22",
|
||||
"nodemon": "^2.0.7",
|
||||
"ts-node": "^9.1.1",
|
||||
"typescript": "^4.1.3"
|
||||
}
|
||||
}
|
||||
17
examples/expressjs/src/index.ts
Normal file
17
examples/expressjs/src/index.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import bodyParser from "body-parser";
|
||||
import express from "express";
|
||||
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3333;
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.raw({ type: "application/vnd.custom-type" }));
|
||||
app.use(bodyParser.text({ type: "text/html" }));
|
||||
|
||||
app.get("/", async (req, res) => {
|
||||
res.json({ Hello: "World" });
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening at http://localhost:${port}`);
|
||||
});
|
||||
13
examples/expressjs/tsconfig.json
Normal file
13
examples/expressjs/tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
|
||||
"incremental": true,
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
}
|
||||
}
|
||||
1266
examples/expressjs/yarn.lock
Normal file
1266
examples/expressjs/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user