initial
This commit is contained in:
commit
74ae93a96e
1 changed files with 94691 additions and 0 deletions
87
joplin notion extension/Web Development/nodejs/Basics.md
Normal file
87
joplin notion extension/Web Development/nodejs/Basics.md
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
Basics
|
||||
|
||||
- express static public
|
||||
```js
|
||||
const express = require("express");
|
||||
const fetch = require("node-fetch");
|
||||
const cors = require("cors");
|
||||
const PORT=process.env.PORT || 8080;
|
||||
const app = express();
|
||||
app.use(express.static(__dirname + "/public"));
|
||||
|
||||
app.use(express.json());
|
||||
const corsOptions = {
|
||||
origin: 'http://example.com',
|
||||
credentials: true,
|
||||
};
|
||||
|
||||
app.use(cors(corsOptions));
|
||||
|
||||
|
||||
const server = app.listen(PORT, () => {
|
||||
console.log(`app is running on port ${PORT}`);
|
||||
|
||||
});
|
||||
|
||||
```
|
||||
- asynchronous vs (multithreading, single threaded)
|
||||
So when an application is single threaded, it can only consume one logical cpu execution context at a time (one thread)
|
||||
This means the logic happens step-by-step (can only be interrupted by the cpu scheduler)
|
||||
|
||||
When an app is multithreaded, it creates child processes via forking or process ownership that it communicates to via shared memory or messaging. Whether the multiple threads perform the same work is up to the design of the application
|
||||
|
||||
asynchronous single threaded : It can perform multiple tasks, not at the same time though.
|
||||
Node's async functionality just lets blocks of code wait for preconditions before it runs -- it's not truly async with other code
|
||||
|
||||
for simultaneoulsy request sometimes
|
||||
In NodeJS, a single instance can use one thread, unless it forks or spawns a worker.
|
||||
Most node webservers use forking, where they effectively spawn one process per logical thread on the host server. They randomly get traffic by a special "port sharing" feature of Node itself -- which is managed by the cluster module IIRC
|
||||
- express get with query
|
||||
```js
|
||||
app.get("/getaudio", async (req, res) => {
|
||||
const queryObj = req.query;
|
||||
console.log("queryobj: ", queryObj);
|
||||
|
||||
const { url, title, q } = queryObj;
|
||||
}
|
||||
}
|
||||
```
|
||||
- express get with params
|
||||
```js
|
||||
app.get('/books/:id', (req,res)=>{
|
||||
const {id}=req.params
|
||||
} )
|
||||
```
|
||||
- express res object
|
||||
- headers
|
||||
`res.setHeaders` - calls the native Node.js method
|
||||
`res.set` - sets headers
|
||||
`res.headers` - an alias to res.set
|
||||
|
||||
```js
|
||||
res.set('Content-Type', 'text/plain');
|
||||
res.set({
|
||||
'Content-Type': 'text/plain',
|
||||
'Content-Length': '123',
|
||||
'ETag': '12345'
|
||||
})
|
||||
|
||||
res.header(field, [value])
|
||||
|
||||
res.setHeader(
|
||||
"Access-Control-Allow-Methods",
|
||||
"GET,OPTIONS,PATCH,DELETE,POST,PUT"
|
||||
);
|
||||
|
||||
```
|
||||
- middleware
|
||||
https://www.geeksforgeeks.org/middleware-in-express-js/
|
||||
|
||||
|
||||
|
||||
to only install production dependencies use
|
||||
`npm install --omit=dev`
|
||||
for only dev dependenciues
|
||||
`npm install --only=dev`
|
||||
for both
|
||||
`npm i`
|
||||
Loading…
Reference in a new issue