darul75/web-react

Using deployd as express middleware

Closed this issue · 15 comments

ktmn commented

I'm trying to use Deployd as middleware for the API using this guide:
http://docs.deployd.com/docs/server/work-with-express.html

Added this to server.js to the "Register middlewares" part

var server = require('http').createServer(app);

require('deployd').attach(server, {
    socketIo: io,  // if not provided, attach will create one for you.
    env: ENV,
    db: {host:'localhost', port:27017, name:'test-app'}
});

I don't know if I need this line, but it doesn't seem to make a difference:

app.use(server.handleRequest);

When I go to :8080/dashboard I get 404... don't know if it's because of how the renderer is set up or deployd didn't attach or what.

Any idea how to get it to work? I really don't want to write the API manually when Deployd does it so well.

Sure I give you a real answer tomorrow

Hi @ktmn

First, nice catch with Deployd as I have never heard about it before and it looks very promising to build robust api.

What you need to do is to use the second part of using this pack.

First part is when you have no own server ( it will use a default one, with no backend activated ) : npm run dev is the command to start it. Good if you only need a client side or one page website. In this case it runs on port 8080. So in your case, your changes in server.js are simply not used.

For you case, use the two following commands.

npm run dev-server-client : this one packages both client and server part.

then

npm run dev-server: this one run your express server with a socket listening for changes on your server or client side.

and see it at http://127.0.0.1:3000

ktmn commented

Ah there are separate commands for client only, client and server, etc. Completely went over my head. I kept using just npm run dev, because npm run dev-server-client produces this error:

C:\Users\User\Documents\Apps\web-react (master)
λ npm run dev-server-client

> web-react@1.0.6 dev-server-client C:\Users\User\Documents\Apps\web-react
> webpack --progress --watch --config conf/webpack-dev-server.js | ./node_modules/.bin/webpack-dev-server --port 8081 --config conf/webpack-dev-server.js --hot --history-api-fallback --progress -colors --inline --content-base ./build

'.' is not recognized as an internal or external command,
operable program or batch file.

I believe that happened before I made any changes to the server.js as well.

and after reading a few, do not forget to start your mongod process, running on 27017 by default.

hmm ok, your are on windows, I have some change to make with a script but no time now.

to fix it replace in package.json following commands

./node_modules/.bin/webpack or ./node_modules/.bin/webpack-dev-server by

webpack and webpack-dev-server

windows do not handle dot . path

and to be sure install webpack and webpack-dev-server globally.

npm install -g webpack
npm install -g webpack-dev-server

sorry I know it is quite annoying

{
"dev": "webpack --config conf/webpack-dev.js | webpack-dev-server --port 8080 --config conf/webpack-dev.js --hot --history-api-fallback --progress --colors --inline --content-base ./build",
    "dev-server-client": "webpack --progress --watch --config conf/webpack-dev-server.js | webpack-dev-server --port 8081 --config conf/webpack-dev-server.js --hot --history-api-fallback --progress --colors --inline --content-base ./build",
    "dev-server": "node build/server.js",
    "dev-server-debug": "node-debug --debug-brk build/server.js --debug --source-maps",
    "build": "webpack --config conf/webpack-prod.js --progress --profile --colors",`
}
ktmn commented

The commands are working now. And it took me a while to figure out I need to run dev-server-client and dev-server simultaneously not one after another lol, but it's working now.

:3000/dashboard still gives me the 404 though.

Don't know if it's the part in renderer.js

let notFound = _.find(state.routes, {isNotFound: true});

if (notFound !== undefined) {
    res.status(404);
}

or something else?

try to put your middleware before this ones in server/server.js

//
// Activate middlewares
// --------------------
app.use(apiRoutes);
app.use(renderer.render);
ktmn commented
app.use(apiRoutes);
app.use(renderer.render);
app.use(server.handleRequest);

^ this get's me the 404, but in this order:

app.use(server.handleRequest);
app.use(apiRoutes);
app.use(renderer.render);

it get's me an error whenever I navigate a page and server crashes

Error loading module node_modules/whatwg-fetch
ReferenceError: self is not defined

which originates from

node_modules\deployd\lib\type-loader.js:26:32

So I guess it's a problem with Deployd?

Heres the full error if it helps:

Error loading module node_modules/whatwg-fetch
ReferenceError: self is not defined
    at C:\Users\User\Documents\Apps\web-react\node_modules\whatwg-fetch\fetch.js:4:7
    at Object.<anonymous> (C:\Users\User\Documents\Apps\web-react\node_modules\whatwg-fetch\fetch.js:335:3)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at C:\Users\User\Documents\Apps\web-react\node_modules\deployd\lib\type-loader.js:26:32
    at process._tickDomainCallback (node.js:381:11)

weird, remove following dependency in my package.json

"whatwg-fetch": "^0.8.1"

if somewhere it fails then, remove call to fetch() I do not remember where it is used..just for samples and small query on client side for demo

ktmn commented

Alright now Deployd works and it even overrides the :3000/ route and displays 404 Resource Not Found.

But at :3000/dashboard there's a working dashboard.

I think I just need deployd for 3 routes, "/dashboard", "/dpd.js" and "/v1/*", rest should be handled by the renderer. Any idea how to set it up like that?

I am not sure I can check now, looks like Deployd uses a public named directory, mine is a dist folder...if you get a repository do not hesitate share it I will look to help you

ktmn commented

Alright thanks for all the help. I think I need to go learn some routing with express.

you are welcome, do not hesitate come back to me