Next.js: middlewares without a custom server or wrappers - javascript

Is it possible to create a Next.js app with middlewares without using a custom server nor wrappers handlers?
When I create an Express app, I split my code into different require statements calling Express middlewares:
const express = require("express");
const app = express();
// I call the functions in each modules to use the different middlewares
require("./startup/cors")(app);
require("./startup/routes")(app);
require("./startup/db")();
const port = process.env.PORT || config.get("port");
const server = app.listen(port, () =>
winston.info(`Listening on port ${port}...`)
);
module.exports = server;
For example, the ./startup/cors module contains the following lines:
const cors = require("cors");
module.exports = function(app) {
app.use(cors());
};
However, with my Next.js app, I don't understand how to get something like this without creating a custom server.
I already came across the article Use middleware in Next.js without custom server but it uses a wrapper solution I would like to avoid.

Currently Next.js supports middlewares only for api paths. There is no support for middlewares in regular pages paths yet.

Related

how can I render the css files when i use express.js?

I am grouping my files like so:
node_modules
structures
{HTML Files}
styles
{CSS Files}
app.js
package-lock.json
package.json
I've already required those:
const express = require('express');
const app = express();
const path = require('path')
what do I do next?
I assume that you're somewhat of a beginner with Node/Express.
I recommend you learn more about how express works before deploying this into an actual app.
Let's get something straight: I believe that you only have a group of HTML files that you want to show to the user under the file names eg. example.com/about.html with the homepage HTML file being called index.html so that express knows what to show where.
This is the simplest way I could think i'd achieve this effect.
const express = require('express');
const app = express();
const path = require('path');
// This is the port where the application is running on
// Uses the server's enviroment variable named PORT if is defined else
// this will use port 5000
// the page can be seen locally at http://localhost:5000/
const PORT = process.env.PORT || 5000;
// This line makes the app created earlier use the middleware that express provides for "rendering" static files
// inside the express.static method we give a path to the static files
// to create that path we use the path.join method provided by the path module we imported earlier
// this method takes in all the paths that need to be joined.
// the __dirname is the directory the application was launced from (you can use process.cwd() to get the root)
// and ofcourse the structures is the folder which contains all your HTML files
app.use(express.static(path.join(__dirname, "structures")));
// Now we do the same thing we did before but we add the middleware for the styles under the "/styles" URI.
app.use("/styles", express.static(path.join(__dirname, "styles")));
// This will start the server at the PORT which we defined earlier.
app.listen(PORT);

jQuery callback function not working for cross domain

I'm using
nodejs in backend at localhost:3000
and
http Apache server at localhost:8080
$.post("http://localhost:3000", {data:"data"}, function(){alert("hurrah");});
// that one not working
$.get("http://localhost:3000", {data:"data"}, function(){alert("hurrah");});
// is working, but callback function not working
Have you checked out Cors for NodeJS?
Cross Domain requests may be blocked to your web server and if you're running NodeJS, you can add Cors(), configure the content you wish to accept (or accept all for the sake of debugging / time at the moment).
Depending on your NodeJS configuration you can set this up by including it at the top of your main class.
A default configuration, using Express might look something like this... (note this accepts all content!)
var express = require('express');
var app = express();
var cors = require('cors');
app.use(cors());
app.listen(8080, () => {
console.log('Listening on 8080');
});

Express performance requiring modules

Having a question regarding the performance of a NodeJS application.
When I have the following express app in NodeJS:
const app = require('express')();
const about = require('./about');
app.use('/about', about);
app.listen(3000, () => console.log('Example app listening on port 3000!'));
My current understanding is that only when the server is started up it does need to require() these files using commonJS modules.
Question:
Does the express application has to execute the require() statements with every request to the server or is this only necessary when starting the server?
Any extra information about how express works under the hood would be nice.
No, those require are only run once when you start the app. It would be different if you include those in the router functions.
app.use('/about', (req, res) => {
const some = require('some');
});
Still in this scenario, modules require are cached so it's not such a big deal.

Convert Nodejs server into node module

Is it possible to convert a nodejs API server into a node module, which can be used for other projects without making much code changes?
Details: There are several API's(get, post, put) in the node js server. So if I use this server as a node module inside another node server, I must be able to access the API's in the node modules directly from the client. Is this possible? If yes, how?
I am required to do a POC on this for a client requirement and so far did not find it possible. Can you please help? I am relatively new to node js development
main script
const express = require('express');
const app = express()
/*
use some middlewares
*/
require('my-module')(app)
const server = http.createServer(app).listen(process.env.PORT);
module.exports = app;
module
module.exports = (app) =>{
app.get('/',(req,res) =>{
res.send('hi im a diffrent module;')
}
}

Javascript variables for Get http

I am learning JavaScript and would like to do the JavaScript equivalent of PHP's $_GET[Var] = $foo; I am coding a basic CDN type server for a project,
also, how can I serve a file for download with JavaScript? The plan is to run this code inside a NodeJS node. Sorry if I explained this badly, I am terrible at explaining things.
To serve existing files from your Node.js app, use express with express.static: http://expressjs.com/en/starter/static-files.html
Example below uses ECMAScript 2015 elements and assumes Node.js 4 or 5 with static files stored in public directory:
const http = require('http');
const express = require('express');
const app = express();
app.use(express.static('public'));
const server = http.createServer(app).listen(8080, serverCallback);
function serverCallback() {
const host = server.address().address;
const port = server.address().port;
console.log(`Server listening on ${host}:${port}`);
}

Categories