#1 connected mangoose and added artist model

parent 80fe2db2
MONGO_URL='mongodb://localhost:27017'
PORT='3000'
\ No newline at end of file
This diff is collapsed.
......@@ -10,10 +10,14 @@
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "^18.15.3",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mongoose": "^7.0.1",
"ts-node-dev": "^2.0.0"
},
"devDependencies": {
"@types/express": "^4.17.17"
"@types/express": "^4.17.17",
"@types/mongoose": "^5.11.97"
}
}
\ No newline at end of file
}
import express, {Express} from 'express';
import express, {Express, json} from 'express';
import mongoose from 'mongoose';
import 'dotenv/config';
import {artistRouter} from './routes/artist';
const app: Express = express();
app.use(json());
app.use('/artists', artistRouter);
app.listen(process.env.PORT, () => {
console.log('Server started on port ' + process.env.PORT);
});
import express, {Request, Response} from 'express';
import {model, Schema, connect, Document, HydratedDocument} from 'mongoose';
const router = express.Router();
interface IArtist {
name: string;
image?: File;
description: string;
}
const artistSchema = new Schema<IArtist>({
name: {
type: String,
required: true,
},
image: {
type: Buffer,
},
description: {
type: String,
required: true,
},
});
const Artist = model<IArtist>('Artist', artistSchema);
const run = async () => {
await connect(`${process.env.MONGO_URL}/artist`);
};
run().catch((err) => console.log(err));
router.get('/', (req: Request, res: Response) => {
return res.send('Hello');
});
router.post('/', async (req: Request, res: Response) => {
const artist: HydratedDocument<IArtist> = new Artist({
name: req.body.name,
description: req.body.description,
});
await artist.save();
res.send(artist);
});
export {router as artistRouter};
declare global {
namespace NodeJS {
interface ProcessEnv {
PORT: string;
MONGO_URL: string;
}
}
}
export {};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment