-
Notifications
You must be signed in to change notification settings - Fork 2
/
Node.js
257 lines (196 loc) · 6.95 KB
/
Node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// NODE.JS - JavaScript runtime environment - by Beumsk
// ------------------------ NODE ------------------------ //
// node . // run index.js
// node // run node in terminal
// node -v // get node version
// in terminal
process.platform; // returns OS -> darwin(mac), win32(windows), linux...
process.version; // node version
process.env.NAME_OF_VARIABLE; // get .env variable value
// via .js file
// GLOBALS
console.log(__dirname); // /full/path/to/file
console.log(__filename); // /full/path/to/file/index.js
// FROM PACKAGE.JSON
// Extract the arguments from process.argv ("node screenshot.js --url=https://www.example.com --output=example.png")
const args = process.argv.slice(2);
const urlArg = args.find((arg) => arg.startsWith('--url='));
const outputArg = args.find((arg) => arg.startsWith('--output='));
// Extract the values from the argument strings
const url = urlArg ? urlArg.split('=')[1] : '';
const outputPath = outputArg ? outputArg.split('=')[1] : '';
// OS
const os = require('os');
const currentOS = {
name: os.type(), // name: 'Darwin',
release: os.release(), // release: '21.6.0',
totalMem: os.totalmem(), // totalMem: 17179869184,
freeMem: os.freemem() // freeMem: 2077073408
}
console.log(currentOS);
// PATH
const path = require('path');
console.log(path.sep); // /
const filePath = path.join('create', '//any', 'path.txt');
console.log(filePath); // create/any/path.txt
console.log(path.basename(filePath)); // path.txt
console.log(path.resolve(__dirname, filePath)); // /full/path/to/create/any/path.txt
// ENV VAR
// in .env file: NAME_OF_VARIABLE=thevalue
require('dotenv').config();
const secret = process.env['NAME_OF_VARIABLE'];
// FILE SYSTEM
const {readFile, readFileSync} require('fs');
const txt = readFileSync('./text.txt', 'utf8'); // default way
readFile('./text.txt', 'uft8', (err, txt) => {console.log(txt)}; // non-blocking way
// OR
const { readFile } require('fs').promises;
async function hello() {const file = await readFile('./text.txt', 'utf8')};
// BODY PARSER
// handle data from a form (<form action="/name" method="post")
let bodyParser = require('body-parser');
app.use('/name', bodyParser.urlencoder({extended: false}));
app.post('/name', function(req, res) {
const name = req.body;
res.json({'name': name.first + ' ' + name.last});
})
// ------------------------ EXPRESS ------------------------ //
let express = require('express');
// import express from 'express';
let app = express();
const PORT = 8000;
app.listen(PORT, () => console.log('Server is running on port ' + PORT));
// METHODS
app.get('/route', function() {});
app.post('/route', function() {});
app.delete('/route', function() {});
app.put('/route', function() {});
// chain methods with this syntax
app.route('/route').get(function() {}).post(function() {});
// ??ACTIONS??
app.get('/', function(req, res) {
res.send('Hello World');
res.sendFile(__dirname + '/index.html'); // needs absolute path
});
app.use('/public', express.static(__dirname + '/public'));
app.get('/json', function(req, res) {
res.json({"message": "Hello json"});
const method = req.method; // GET, POST, DELETE, PUT
const path = req.path; // for example /json
const ip = req.ip; // ip address
});
app.get('/user/:userId', function(req, res) { // /user/543
const params = req.params; // route parameters {userId: '543'}
res.json({'userId': params.userId});
});
app.get('/user', function(req, res) { // /user?userId=543
const query = req.query; // route query {userId: '543'}
res.json({'userId': query.userId});
});
// ------------------------ MONGODB ------------------------ //
// https://www.freecodecamp.org/news/get-started-with-mongodb-atlas/
// https://www.freecodecamp.org/news/introduction-to-mongoose-for-mongodb-d2a7aa593c57/
// New project
// new DB (or cluster)
// new user with 'read and write to any database' (security > database access)
// access all API (security > network access)
// connect (deployment > database) => mongodb+srv://<username>:<password>@<cluster-name>.prx1c.mongodb.net/<db-name>?retryWrites=true&w=majority
const mongoose = require('mongoose');
mongoose.connect('mongodb+srv://<username>:<password>@<cluster-name>.fnwaqdz.mongodb.net/?retryWrites=true&w=majority', { useNewUrlParser: true, useUnifiedTopology: true })
// create schema
const personSchema = new mongoose.Schema({
name: { type: String, required: true },
age: Number,
favoriteFoods: [String]
});
const Person = mongoose.model('Person', personSchema);
// create record and save
const createAndSavePerson = (done) => {
var johnDoe = new Person({name: "John Doe", age: 34, favoriteFoods: ['egg', 'meat']});
johnDoe.save((err, data) => {
if (err) return console.error(err);
done(null , data);
})
};
// create many records
const arrayOfPeople = [
{name: "Jane", age: 38, favoriteFoods: ['salad']},
{name: "Jude", age: 31, favoriteFoods: ['fruits']}
];
const createManyPeople = (arrayOfPeople, done) => {
Person.create(arrayOfPeople, (err, data) => {
if (err) return console.error(err);
done(null , data);
})
};
// find records
const personName = "Jane";
const findPeopleByName = (personName, done) => {
Person.find({name: personName}, (err, data) => {
if (err) return console.error(err);
done(null , data);
});
};
// find one record
const food = ['salad'];
const findOneByFood = (food, done) => {
Person.findOne({favoriteFoods: food}, (err, data) => {
if (err) return console.error(err);
done(null , data);
});
};
// find by _id
const personId = 1;
const findPersonById = (personId, done) => {
Person.findById(personId, (err, data) => {
if (err) return console.error(err);
done(null , data);
});
};
// classic way of updating
const foodToAdd = "hamburger";
const findEditThenSave = (personId, done) => {
Person.findById(personId, (err, data) => {
if(err) return console.log(err);
data.favoriteFoods.push(foodToAdd);
data.save((err, updatedPerson) => {
if(err) return console.log(err);
done(null, updatedPerson)
});
});
};
// new way of updating (similar: findByIdAndUpdate)
const ageToSet = 20;
const findAndUpdate = (personName, done) => {
Person.findOneAndUpdate({name: personName}, {age: ageToSet}, { new: true }, (err, data) => {
if(err) return console.log(err);
done(null , data);
});
};
// rmove by _id (similar: findOneAndRemove)
const removeById = (personId, done) => {
Person.findByIdAndRemove(personId, (err, data) => {
if(err) return console.log(err);
done(null , data);
});
};
// remove
const removeManyPeople = (done) => {
const nameToRemove = "Mary";
Person.remove({name: nameToRemove}, (err, data) => {
if(err) return console.log(err);
done(null , data);
});
};
// chain search query helpers
const queryChain = (done) => {
const foodToSearch = "burrito";
Person.find({favoriteFoods: foodToSearch})
.sort({name: 1})
.limit(2)
.select({age: 0})
.exec((err, data) => {
if(err) return console.log(err);
done(null , data);
})
};