Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
this is a simple, fast light weight implementation of tools to help create a server based web application without using any 3rd party library, other than the core libraries provided by node.
this is a simple light weight implementation of tools to help create a server based web application without using any 3rd party library, other than the core libraries provided by node.
node 15
module
create an express like server using this class. inherits from Router. so all the functionalities of router is also applicable for server
import Server from 'sp-wt/modules/server.js'
//create server object
let app = new Server();
//add routes (see router class for more)
app.get('/', (req, res) => {
res.send("hello world");
});
//listen to port
app.listen(3000, () => { console.log('server listening at port 3000') });
sets a value with the key name that can be accsess from req.app.get(name)
get a value set in the server
set middlewares and handler for GET requests on the routeUrl path the order of the function determines which function is called before other one. handler is called at the end. if next() is not called from the middleware the upocming middlewares and handler will not be called.
see other available methods is Router section
a callback function with three arguments req, res, next
function middleware (req, res, next) {
if(isAuthenticated) // demo if condition add your own logic
next()
else
res.json({error: 'not authenticated'})
}
a callback function with two arguments req and res
function handler (req, res) {
let name = req.body.name;
res.json({userName : name})
}
req is a modified http.IncomingMessage object which has several fields and methods on top of the standard ones
res is a modified http.ServerResponse object which has several methods on top of the standard ones
res.redirect(path, statusCode?) : redirects request to the given path
res.send(data) :sends data as rensponse, data can be anything
res.sendFile(filepath) :send the file mentioned in the filepath as response
res.status(code) : sets the code for the response statusCode
res.header(name, value) :sets header with name as key for value
res.type(t) : sets the correct mimetype t for for content-type header (e.g. res.type('json'))
res.contentType(t) : set content-type header to t
res.json(object) : sends javascript object object as json as response
res.setCookies(cookies) : set cookies for the response
res.render(viewTemplateName, props): renders correspondent view template using RenderEngine with supplied props at serverside and sends as html response
instantiates a new Router object
instantiates a new StaticRouter object
listens to a given port, reuturns internal http server function overloads-
create an express like router using this class. server itself is an router so all router methods are available for server
import Server from 'sp-wt/modules/server.js'
import Router from 'sp-wt/modules/router.js'
//create server object
let app = new Server();
//create a router object
let router = new Router();
//add all routes
router.post('/pokemon/:id', (req, res) => {
res.json({
params: req.params,
body: req.body
});
});
//use router for anime path, i.e. POST to /anime/pokemon/2
app.use('/anime', router);
sets middleware for all routes in the router. these middlewares are called first ,before the middlewares in the hendler mothods like get, post etc. order of middleware is depends on declearation of use in code
set another router object to handle all the subroutes in the routePath
set middlewares and handler for all kinds of requests(e.g. GET, POST etc.) on the routeUrl path
set middlewares and handler for GET requests on the routeUrl path
set middlewares and handler for POST requests on the routeUrl path
set middlewares and handler for PUT requests on the routeUrl path
set middlewares and handler for DELETE requests on the routeUrl path
set middlewares and handler for PATH requests on the routeUrl path
serve files folder and all it's subfolders with same path relative to folder constructor takes path to the folder as argument should be used as a Router
import Server from 'sp-wt/modules/server.js'
import StaticRouter from 'sp-wt/modules/staticRouter.js'
//create server object
let app = new Server();
//given that all files you want to serve are present in public folder in same directory
//i.e. public/pages/about.html will be served at localhost:3000/pages/about.html
app.use('/', new StaticRouter(__dirname +'/public'))
//listen to port
app.listen(3000, () => { console.log('server listening at port 3000') });
an internal class for serverside renderring
file structure
views/page.htm
app.js
./views/page.htm
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>hello world</h1>
<h2>{{this.name}}</h2>
<ul>
{{for (let p of this.pokemons) { }}
<li>name: <b>{{p.name}}</b>, ability: <b>{{p.ability}}</b> </li>
{{ } }}
</ul>
<div>
<input type="text" id="msg"/>
<button id="btn">Button</button>
</div>
</body>
./app.js
import Server from 'sp-wt/modules/server.js'
import StaticRouter from 'sp-wt/modules/staticRouter.js'
//create server object
let app = new Server();
//set the folder for view templates
app.set('view folder', './views')
// render page.htm template at localhost:3000/
app.get('/', (req, res) => {
console.log(process.env.DATABASE);
res.render('page', {
name: 'spandan mondal',
pokemons: [
{
name: 'pikachu',
type: 'electic',
ability: 'static'
},
{
name: 'charmender',
type: 'fire',
ability: 'blaze'
},
{
name: 'ratatta',
type: 'normal',
ability: 'run away, guts, hustle'
}
]
})
});
//listen to port
app.listen(3000, () => { console.log('server listening at port 3000') });
res.render(templateName, renderprops)
use double curly braces to get access values of renderprops. props are acccessable under this
{{ this.property }}
any valid javascript expression under {{ }} will be pre calcuated before rendering
{{ 2 + 2 }} -> 4
acts like normal if-else. javascript part should be in {{ }}
{{ if (condition_1 ) { }} < html to render if condition_1 is true > {{ } else if (condition_2) { }} < html to render if condition_2 is true > {{ } else { }} < html to render if condition_1 and condition_2 are false > {{ } }}
acts like normal for loop. javascript part should be in {{ }}
{{for (let i = 0; i < 10; i++ >) { }} < print this html 10 times > {{ } }}
same as if-else. just keep javascript part in {{ }}
use this to include a template in another template. properties will be accessable from included templates too
card.htm
<div class="card">
<h3>{{ c.title }}</h3>
<p>{{ c.description }}</p>
</div>
page.htm
<body>
{{for (let c of this.cards ){ }}
{{ @include <card> }}
{{ } }}
</body>
javascript
res.render('page', { cards : [
{title: 'title 1', description: 'description 1'},
{title: 'title 2', description: 'description 2'},
{title: 'title 3', description: 'description 2'}
]})
.env
key1=value1
key2 = value2
key3 = "value3"
javascript
import env from '../modules/env.js';
//call to set environment variables
env();
console.log(process.env.key1);
set of functions to add essential middlewares to server app
import Server from '../modules/server.js';
import { json, urlencoded, formdata, cros } from '../modules/commonMiddlewares.js';
let app = new Server();
//enable cross origin requests
app.use(cros());
//parse json requests and put data object as req.body
app.use(json());
//parse urlencoded requests and put fields into req.body boject
app.use(urlencoded());
//parse multipart/formdata requests
// put text fields into req.body object
// put files into req.files array
// every object in req.files array contain
// fieldName : name of the input field,
// fileName : name of the uploaded file,
// contentType : mimetype of the uploaded file,
// extension : extension of the uploaded file,
// buffer : Buffer object containing binary data of the file
app.use(formdata());
create and verrify json web tokens for authentication
import JWT from 'sp-wt/modules/jsonWebToken.js'
//instanciate jwt object
let jwt = new JWT();
//create jwt token
let token = jwt.sign({user: 'ash', age: 10}, 'secret_of_ash')
let v = jwt.verify(token, 'secret_of_ash')
if(v) {
const {user, age} = v;
}else {
console.log('invalid token');
}
used to create jwt tokens
used to verify a token. if the token is valid returns payload else returns false.
create and use websocket server for realtime socket connection with websocket api provided by client browser get httpServer from app.httpServer where app is an object of Server class
import Server from '../modules/server.js';
import WebSocket from '../modules/webSocket.js';
let app = new Server();
//create websocket instance
let ws = new WebSocket(app.httpServer);
//listen to client connection
ws.onConnect = (socket, sid) => {
console.log('connected id: ' + sid );
ws.send(socket, {
message: 'connected to server'
});
};
ws.onTextMessage = (data, socket, sid) => {
console.log(data);
}
app.listen(3000, () => { console.log('server listening at port 3000') });
callback that is called when client connects
(socket, sid) => {} socket represents the connection socket of the client. store the socket to use it to send message data to client. sid represents unique uuid assigned to the client
callback that is called when client disconnects
(socket, sid) => {} socket represents the connection socket of the client. sid represents unique uuid assigned to the client
callback that is called when client sends a message
(data, socket, sid) => {}; data contains the message as raw socket represents the client socket. use it to identify client sid represents unique uuid assigned to the client
callback that is called when client sends a message
(data, socket, sid) => {}; data contains the message in text format socket represents the client socket. use it to identify client sid represents unique uuid assigned to the client
function to send message to client socket prepersents client socket that you want to send data obj represent the javascript object you want to send as json
function to send message to client sid prepersents unique client uuid that you want to send data obj represent the javascript object you want to send as json
function to send message to client socket prepersents client socket that you want to send data text represent the string you want to send
function to send message to client sid prepersents unique client uuid that you want to send data text represent the string you want to send
close connection with client socket
close connection with client with sid as unique id
connect to postgresql sql database, execute queries, get results. can be used in both async and callback mode. connection and disconnection is innitiated automatically when you try to execute query.
import PostgreSql from "../modules/postgres.js"
console.log("database test")
let pg = new PostgreSql({
host: "localhost",
user: "postgres",
password: "spandan9733",
database: "testdb"
});
async function main() {
//databse query using callback
pg.query("select * from items", (err, result) => {
if(err)
console.log(err);
let row;
//getting one row at a time
while(row = result.fetch_assoc()) {
console.log(row);
}
});
//database query using async and arguments
try {
let res = await pg.query("select * from items where id = $1", [1]);
console.log(res);
} catch (e) {
console.log(e);
}
}
main();
sql represents the sql query callback represent a fucntion that is called when the query is executed. (optional)
returns a javascript promise containing result object
the callback function looks like
(err, result) => {}
if there is error in execution err will contain the error, else it will be null for successful query and the result object will contain output of the query
contains different fields and method
returns an prepareStatement object with functions to make secure sql queries
callback for successful connection to database
callback for successfully disconnecting from database
callback for error handling, has one argument containing error
serverside implementation of fetch function to make http calls. By default it makes GET request.
options
FAQs
this is a simple, fast light weight implementation of tools to help create a server based web application without using any 3rd party library, other than the core libraries provided by node.
We found that sp-wt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.